ios获取相册图片 压缩图片
从摄像头/相册获取图片
刚刚在上面的知识中提到从摄像头/相册获取图片是面向终端用户的,由用户去浏览并选择图片为程序使用。在这里,我们需要过UIImagePickerController类来和用户交互。
使用UIImagePickerController和用户交互,我们需要实现2个协议。
代码如下
#pragma mark 从用户相册获取活动图片
- (void)pickImageFromAlbum
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;//带裁剪框
[self presentModalViewController:imagePicker animated:YES];
}
我们来看看上面的从相册获取图片,我们首先要实例化UIImagePickerController对象,然后设置imagePicker对象为当前对象,设置imagePicker的图片来源为UIImagePickerControllerSourceTypePhotoLibrary,表明当前图片的来源为相册,除此之外还可以设置用户对图片是否可编辑。
代码如下
#pragma mark 从摄像头获取活动图片
- (void)pickImageFromCamera
{
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;
imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
imagePicker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
imagePicker.allowsEditing = YES;
[self presentModalViewController:imagePicker animated:YES];
}
以上是从摄像头获取图片,和从相册获取图片只是图片来源的设置不一样,摄像头图片的来源为UIImagePickerControllerSourceTypeCamera。
在和用户交互之后,用户选择好图片后,会回调选择结束的方法。
代码如下
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *image= [info objectForKey:@"UIImagePickerControllerOriginalImage"];
if (picker.sourceType == UIImagePickerControllerSourceTypeCamera)
{
// UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}
theImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(120.0, 120.0)];
UIImage *midImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)];
UIImage *bigImage = [UtilMethod imageWithImageSimple:image scaledToSize:CGSizeMake(440.0, 440.0)];
[theImage retain];
[self saveImage:theImage WithName:@"salesImageSmall.jpg"];
[self saveImage:midImage WithName:@"salesImageMid.jpg"];
[self saveImage:bigImage WithName:@"salesImageBig.jpg"];
[self dismissModalViewControllerAnimated:YES];
[self refreshData];
[picker release];
}
在回调结束的方法中,我们对图片进行了大小的处理,为图片的上传做准备。
缩放图片
缩放图片比较简单,就直接放上代码,让大家参考一下。
代码如下
//压缩图片 带裁剪框
+ (UIImage*)imageWithImageSimple:(UIImage*)image scaledToSize:(CGSize)newSize
{
// Create a graphics image context
UIGraphicsBeginImageContext(newSize);
// Tell the old image to draw in this new context, with the desired
// new size
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Get the new image from the context
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
// End the context
UIGraphicsEndImageContext();
// Return the new image.
return newImage;
}
iOS 压缩图片 如下
//压缩图片尺寸 不带裁剪框
float scales = image.size.height / image.size.width;
UIImage *normalImg;
NSData *newData;
//(一)
/*
如果需要改动被压大小,调整scale,而不是kk或aa
*/
if (image.size.width > 1000 || image.size.height > 1000) {//这里的1000就是scale,所有的都要随着改变
if (scales > 1) {
CGSize newSize = CGSizeMake(1000 / scales, 1000);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
normalImg = UIGraphicsGetImageFromCurrentImageContext();
}else {
CGSize newSize = CGSizeMake(1000 ,1000 * scales);
UIGraphicsBeginImageContext(newSize);
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
normalImg = UIGraphicsGetImageFromCurrentImageContext();
}
}
else {
normalImg=image;
}
//(二)
CGSize newSize = CGSizeMake(normalImg.size.width, normalImg.size.height);
UIGraphicsBeginImageContext(newSize);
[normalImg drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext()
//图片压缩系数
float kk = 1.0f;
//图片压缩系数变化步长(可变)
float aa = 0.1f;
//压缩后的大小
int mm;
mm=(int)UIImageJPEGRepresentation(newImage, kk).length;
while (mm/1024 > 100) {
if (kk > aa+aa/10) {
kk -= aa;
mm = (int)UIImageJPEGRepresentation(newImage, kk).length;
}else{
aa /= 10;
}
}
newData = UIImageJPEGRepresentation(newImage, kk);//最后压缩结果
NSLog(@"11111------------===%ld",(long)newData.length/1024);
if (newData.length/1024 > 100) {
return nil;
}else{
UIImage *image = [UIImage imageWithData:newData];
return image;
}
}
ios获取相册图片 压缩图片的更多相关文章
- ios中摄像头/相册获取图片压缩图片上传服务器方法总结
本文章介绍了关于ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结,有需要了解的同学可以参考一下下. 这几天在搞iphone上面一个应用的开发,里面有需要摄像头/相册编程和图片上传的问 ...
- iOS获取相册/相机图片-------自定义获取图片小控件
一.功能简介 1.封装了一个按钮,点击按钮,会提示从何处获取图片:如果设备支持相机,可以从相机获取,同时还可以从手机相册获取图片. 2.选择图片后,有一个block回调,根据需求,将获得的图片拿来使用 ...
- iOS遍历相册中的图片
//获取相册的所有图片 - (void)reloadImagesFromLibrary { self.images = [[NSMutableArray alloc] init]; dispatch_ ...
- webpack2使用ch10-处理图片(png jpg svg 等) 限制图片 压缩图片
1 目录展示 安装依赖 "file-loader": "^0.11.1", "image-webpack-loader": "^3 ...
- 异步上传&预览图片-压缩图片
移动端普及的时代,流量是用户最关心的,手机拍出来的照片基本上都在1~2M以上,这样上传会非常耗流量,影响用户体验,此例能在保证清晰度的情况下,将4.5M的图片压缩为30K <!DOCTYPE h ...
- iOS 获取相册中图片的名字 url
__block NSString *imageFileName; NSURL *imageURL = [info valueForKey:UIImagePickerControllerReferenc ...
- IOS 图片上传处理 图片压缩 图片处理
- (void)initActionSheet { UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil dele ...
- ply python 图片压缩 图片裁剪 旋转
http://tech.seety.org/python/python_imaging.html
- (转)Android学习-使用Async-Http实现图片压缩并上传功能
(转)Android学习-使用Async-Http实现图片压缩并上传功能 文章转载自:作者:RyaneLee链接:http://www.jianshu.com/p/940fc7ba39e1 让我头疼一 ...
随机推荐
- 【Linux学习三】Linux系统目录架构
主要包括: ●bin:保存的是可执行文件,二进制,就是命令 ●boot:引导目录,操作系统的启动加载,包含版本内核文件.greb引导程序- ●dev:硬件设备文件,如硬盘.网卡.声卡.终端.显卡,每一 ...
- 前端学习——ionic/AngularJs——获取验证码倒计时按钮
按钮功能为:点击"获取验证码"--按钮不可用-设置倒计时-60秒后重新获取. 代码借鉴于:http://plnkr.co/edit/Swj82MpJSix3a47jZRHP?p= ...
- InnoDB的Named File Formats
随着InnoDB存储引擎的发展,新的页数据结构有时用来支持新的功能特性.比如前面提到的InnoDB Plugin,提供了新的页数据结构来支持表压缩功能,完全溢出的(Off page)大变长字符类型字段 ...
- 如何用php开启企业微信开发的回调模式
猜想: 懵逼 实践: 微信公众号开发的手册中甚至给出了只需要修改几个参数就能使用的范例.企业微信开发中在一个很不显眼的地方放了一个sample. https://work.weixin.qq.com/ ...
- oracle建表的时候同时创建主键,外键,注释,约束,索引
--主键create table emp (id number constraint id_pr primary key ,name1 varchar(8));create table emp9 (i ...
- 中兴电信光纤猫F450获取管理员密码方法
初衷:为了完成端口映射,一开始以为电信光猫不支持自定义路由,因为通过useradmin登录进去后没有找到对应的选项.一番了解之后,原来光猫有超级密码,电信装机时是不会告诉你的,电信客服一般也不会告诉你 ...
- linux命令之ifconfig详细解释
依赖于ifconfig命令中使用一些选项属性,ifconfig工具不仅可以被用来简单地获取网络接口配置信息,还可以修改这些配置. 1.命令格式: ifconfig [网络设备] [参数] 2.命令功能 ...
- webkit框架的使用
// // JSViewController.m // Library // // Created by 朱逸 on 16/7/7. // Copyright © 2016年 朱逸. All righ ...
- 浙大pat1009题解
1009. Product of Polynomials (25) 时间限制 400 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yu ...
- 2014年蓝桥杯预选赛 C/C++ 本科A组试题--切面条
//主要是要找到f(n)=2*f(n-1)-1的规律. #include <stdio.h> #include <math.h> int f(int n) { if(n==0) ...