关于UIImagePickerController读取照片时的卡顿和照片压缩
2014-12-31 18:15
225 查看
UIImagePickerController
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//关闭相册界面 //这个位置解决使用图片时的内存徒增的现象
[picker dismissViewControllerAnimated:YES
completion:nil];
NSString *type = [info
objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info
objectForKey:@"UIImagePickerControllerOriginalImage"];
//图片比例压缩的一套方法
CGSize size = [LW_ImageCompression
get_ImageCompressionProportion:image];
image = [image imageByScalingAndCroppingForSize:size];
NSData *data =
UIImagePNGRepresentation(image);
if (data ==
nil)
{
data = UIImageJPEGRepresentation(image,
0.25);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager
defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中
并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
[fileManager createFileAtPath:[DocumentsPath
stringByAppendingString:@"/image.png"]
contents:data attributes:nil];
//GET THE IMAGE
}
}
class :
LW_ImageCompression
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
+(CGSize)get_ImageCompressionProportion:(UIImage *)image
{
CGSize size = image.size;
CGFloat HBL = ScreenHeight / size.height;
CGFloat WBL = ScreenWidth / size.width;
if (size.width <=
ScreenWidth && size.height <=
ScreenHeight) {
return size;
}
else if (size.width >
ScreenWidth && size.height <=
ScreenHeight) {
size.width =
ScreenWidth;
size.height = size.height * WBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.height];
size.height = [str
floatValue];
}
else if (size.height >
ScreenHeight && size.width <=
ScreenWidth) {
size.height =
ScreenHeight;
size.width = size.width * HBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.width];
size.width = [str
floatValue];
}
else if (size.height >
ScreenHeight && size.width >
ScreenWidth) {
if (HBL < WBL) {
size.height =
ScreenHeight;
size.width = size.width * HBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.width];
size.width = [str
floatValue];
}
else
{
size.width =
ScreenWidth;
size.height = size.height * WBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.height];
size.height = [str
floatValue];
}
}
return size;
}
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
//关闭相册界面 //这个位置解决使用图片时的内存徒增的现象
[picker dismissViewControllerAnimated:YES
completion:nil];
NSString *type = [info
objectForKey:UIImagePickerControllerMediaType];
//当选择的类型是图片
if ([type isEqualToString:@"public.image"])
{
//先把图片转成NSData
UIImage* image = [info
objectForKey:@"UIImagePickerControllerOriginalImage"];
//图片比例压缩的一套方法
CGSize size = [LW_ImageCompression
get_ImageCompressionProportion:image];
image = [image imageByScalingAndCroppingForSize:size];
NSData *data =
UIImagePNGRepresentation(image);
if (data ==
nil)
{
data = UIImageJPEGRepresentation(image,
0.25);
}
//图片保存的路径
//这里将图片放在沙盒的documents文件夹中
NSString * DocumentsPath = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
//文件管理器
NSFileManager *fileManager = [NSFileManager
defaultManager];
//把刚刚图片转换的data对象拷贝至沙盒中
并保存为image.png
[fileManager createDirectoryAtPath:DocumentsPath
withIntermediateDirectories:YES
attributes:nil
error:nil];
[fileManager createFileAtPath:[DocumentsPath
stringByAppendingString:@"/image.png"]
contents:data attributes:nil];
//GET THE IMAGE
}
}
class :
LW_ImageCompression
#define ScreenHeight [UIScreen mainScreen].bounds.size.height
#define ScreenWidth [UIScreen mainScreen].bounds.size.width
+(CGSize)get_ImageCompressionProportion:(UIImage *)image
{
CGSize size = image.size;
CGFloat HBL = ScreenHeight / size.height;
CGFloat WBL = ScreenWidth / size.width;
if (size.width <=
ScreenWidth && size.height <=
ScreenHeight) {
return size;
}
else if (size.width >
ScreenWidth && size.height <=
ScreenHeight) {
size.width =
ScreenWidth;
size.height = size.height * WBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.height];
size.height = [str
floatValue];
}
else if (size.height >
ScreenHeight && size.width <=
ScreenWidth) {
size.height =
ScreenHeight;
size.width = size.width * HBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.width];
size.width = [str
floatValue];
}
else if (size.height >
ScreenHeight && size.width >
ScreenWidth) {
if (HBL < WBL) {
size.height =
ScreenHeight;
size.width = size.width * HBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.width];
size.width = [str
floatValue];
}
else
{
size.width =
ScreenWidth;
size.height = size.height * WBL;
NSString * str = [NSString
stringWithFormat:@"%.0f",size.height];
size.height = [str
floatValue];
}
}
return size;
}
相关文章推荐
- 关于UIImagePickerViewController拍摄video的分辨率和方向的一些代码整理
- UI_UIImagePickerController(读取图片)
- 关于UIImagePickerController 被dismiss 后 屏幕顶部弹出红条 的问题
- 修改 UIImagePickerController 选择照片状态栏颜色及导航栏按钮标题字体颜色
- 视频录制,照片拍摄,保存等,UIImagePickerController
- 关于UIImagePickerController的简单使用
- UIImagePickerController  本地照片
- UIImagePickerController 相册取照片
- UIImagePickerController获取照片的实现,添加overlay方法
- 关于Unity3D(4.7和5.3.4)与IOS原生Object-C交互,调用IOS原生UIImagePickerController的问题
- Swift 读取系统图片以及改变图片 圆形 UIImagePickerController
- UIImagePickerController 选照片 拍照 录视频
- Swift - 使用UIImagePickerController从相册选择照片并展示
- UIImagePickerController关于图片裁剪
- 关于将系统自带的UIImagePickerController 作为uitabbarcontroller时 present时出现问题的解决
- 06-UIImagePickerController 照片单选
- ios中关于UIImagePickerController的一些知识总结
- 获取手机照片UIImagePickerViewController
- Swift - 从相册中选择照片并上传(使用UIImagePickerController)
- ios 调用系统相机,图库,相册。将照片保存在相册,保存图片在本地,UIImagePickerController