打开APP
未登录
开通VIP,畅享免费电子书等14项超值服
开通VIP
首页
好书
留言交流
下载APP
联系客服
UIImagePickerController编辑图片框大小的问题 | iOS开发
最初九月雪
>《iOS》
2015.08.07
关注
Apple Dev Forums
This includes most of what you need, and takes care of all the camera orientation issues. I've added the following which will take in the editing info and use it to get the original cropping rect with this addition:
复制代码
- (UIImage*)scaleImage:(UIImage*)anImage withEditingInfo:(NSDictionary*)editInfo{
UIImage *newImage;
UIImage *originalImage = [editInfo valueForKey:@"UIImagePickerControllerOriginalImage"];
CGSize originalSize = CGSizeMake(originalImage.size.width, originalImage.size.height);
CGRect originalFrame;
originalFrame.origin = CGPointMake(0,0);
originalFrame.size = originalSize;
CGRect croppingRect = [[editInfo valueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
CGSize croppingRectSize = CGSizeMake(croppingRect.size.width, croppingRect.size.height);
CGSize croppedScaledImageSize = anImage.size;
float scaledBarClipHeight = 80;
CGSize scaledImageSize;
float scale;
if(!CGSizeEqualToSize(croppedScaledImageSize, originalSize)){
scale = croppedScaledImageSize.width/croppingRectSize.width;
float barClipHeight = scaledBarClipHeight/scale;
croppingRect.origin.y -= barClipHeight;
croppingRect.size.height += (2*barClipHeight);
if(croppingRect.origin.y<=0){
croppingRect.size.height += croppingRect.origin.y;
croppingRect.origin.y=0;
}
if(croppingRect.size.height > (originalSize.height - croppingRect.origin.y)){
croppingRect.size.height = (originalSize.height - croppingRect.origin.y);
}
scaledImageSize = croppingRect.size;
scaledImageSize.width *= scale;
scaledImageSize.height *= scale;
newImage = [self cropImage:originalImage to:croppingRect andScaleTo:scaledImageSize];
}else{
newImage = originalImage;
}
return newImage;
}
I updated the call back method from the dev forums post to the following:
复制代码
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
[self dismissModalViewControllerAnimated:YES];
self.myImageView.userInteractionEnabled=YES;
CGRect imageFrame = myImageView.frame;
CGPoint imageCenter = myImageView.center;
UIImage *croppedImage;
NSMutableDictionary *imageDescriptor = [editInfo mutableCopy];
// CGFloat scaleSize = 400.0f;
CGFloat scaleSize = 640.0f;
switch ([picker sourceType]) {
//done
case UIImagePickerControllerSourceTypePhotoLibrary:
croppedImage = [self scaleImage:img withEditingInfo:editInfo];
[imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
break;
case UIImagePickerControllerSourceTypeCamera: {
UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
if (originalOrientation != UIImageOrientationUp) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect origRect;
[[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
CGFloat scale = scaleSize/1600.0f;
origRect.origin.x *= scale;
origRect.origin.y *= scale;
origRect.size.width *= scale;
origRect.size.height *= scale;
croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
[imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
[pool drain];
}
else {
croppedImage = [self scaleImage:img withEditingInfo:editInfo];
[imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
}
}
break;
case UIImagePickerControllerSourceTypeSavedPhotosAlbum: {
UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
if (originalOrientation != UIImageOrientationUp) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
CGRect origRect;
[[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
CGFloat scale = scaleSize/640.0f;
origRect.origin.x *= scale;
origRect.origin.y *= scale;
origRect.size.width *= scale;
origRect.size.height *= scale;
croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
[imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
[pool drain];
}
else {
croppedImage = [self scaleImage:img withEditingInfo:editInfo];
[imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
}
}
break;
default:
break;
}
imageFrame.size = croppedImage.size;
myImageView.frame = imageFrame;
myImageView.image = [imageDescriptor objectForKey:@"croppedImage"];
myImageView.center = imageCenter;
}
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报
。
打开APP,阅读全文并永久保存
查看更多类似文章
猜你喜欢
类似文章
【热】
打开小程序,算一算2024你的财运
1段“四女侍一夫”视频曝光,引全网热议:咋这么疯?
世界遗产名录
演员霍思燕:我这辈子最正确的决定,就是在33岁为丈夫生儿子
猪蹄炖不烂,只要加上这5种香料,保证猪蹄软糯脱骨,满口留香
超详细的结构分析
1味中药虎虎生威!把肝胆肠胃里的淤堵,全都清理掉
更多类似文章 >>
生活服务
热点新闻
留言交流
回顶部
联系我们
分享
收藏
点击这里,查看已保存的文章
导长图
关注
一键复制
下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!
联系客服
微信登录中...
请勿关闭此页面
先别划走!
送你5元优惠券,购买VIP限时立减!
5
元
优惠券
优惠券还有
10:00
过期
马上使用
×