打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
UIImagePickerController编辑图片框大小的问题 | iOS开发
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:
复制代码
  1. - (UIImage*)scaleImage:(UIImage*)anImage withEditingInfo:(NSDictionary*)editInfo{
  2.     UIImage *newImage;
  3.     UIImage *originalImage = [editInfo valueForKey:@"UIImagePickerControllerOriginalImage"];
  4.     CGSize originalSize = CGSizeMake(originalImage.size.width, originalImage.size.height);
  5.     CGRect originalFrame;
  6.     originalFrame.origin = CGPointMake(0,0);
  7.     originalFrame.size = originalSize;
  8.     CGRect croppingRect = [[editInfo valueForKey:@"UIImagePickerControllerCropRect"] CGRectValue];
  9.     CGSize croppingRectSize = CGSizeMake(croppingRect.size.width, croppingRect.size.height);
  10.     CGSize croppedScaledImageSize = anImage.size;
  11.     float scaledBarClipHeight = 80;
  12.     CGSize scaledImageSize;
  13.     float scale;
  14.     if(!CGSizeEqualToSize(croppedScaledImageSize, originalSize)){
  15.         scale = croppedScaledImageSize.width/croppingRectSize.width;
  16.         float barClipHeight = scaledBarClipHeight/scale;
  17.         croppingRect.origin.y -= barClipHeight;
  18.         croppingRect.size.height += (2*barClipHeight);
  19.         if(croppingRect.origin.y<=0){
  20.             croppingRect.size.height += croppingRect.origin.y;
  21.             croppingRect.origin.y=0;
  22.         }
  23.         if(croppingRect.size.height > (originalSize.height - croppingRect.origin.y)){
  24.             croppingRect.size.height = (originalSize.height - croppingRect.origin.y);
  25.         }
  26.         scaledImageSize = croppingRect.size;
  27.         scaledImageSize.width *= scale;
  28.         scaledImageSize.height *= scale;
  29.         newImage =  [self cropImage:originalImage to:croppingRect andScaleTo:scaledImageSize];
  30.     }else{
  31.         newImage = originalImage;
  32.     }
  33.     return newImage;
  34. }

I updated the call back method from the dev forums post to the following:
复制代码
  1. - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo {
  2.     [self dismissModalViewControllerAnimated:YES];
  3.     self.myImageView.userInteractionEnabled=YES;
  4.     CGRect imageFrame = myImageView.frame;
  5.     CGPoint imageCenter = myImageView.center;
  6.     UIImage *croppedImage;
  7.     NSMutableDictionary *imageDescriptor = [editInfo mutableCopy];
  8.     // CGFloat scaleSize = 400.0f;
  9.     CGFloat scaleSize = 640.0f;
  10.     switch ([picker sourceType]) {
  11.             //done
  12.         case UIImagePickerControllerSourceTypePhotoLibrary:
  13.             croppedImage = [self scaleImage:img withEditingInfo:editInfo];
  14.             [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
  15.             break;
  16.         case UIImagePickerControllerSourceTypeCamera: {
  17.             UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
  18.             if (originalOrientation != UIImageOrientationUp) {
  19.                 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  20.                 CGRect origRect;
  21.                 [[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
  22.                 UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
  23.                 CGFloat scale = scaleSize/1600.0f;
  24.                 origRect.origin.x *= scale;
  25.                 origRect.origin.y *= scale;
  26.                 origRect.size.width *= scale;
  27.                 origRect.size.height *= scale;
  28.                 croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
  29.                 [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
  30.                 [pool drain];
  31.             }
  32.             else {
  33.                 croppedImage = [self scaleImage:img withEditingInfo:editInfo];
  34.                 [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
  35.             }
  36.         }
  37.             break;
  38.         case UIImagePickerControllerSourceTypeSavedPhotosAlbum: {
  39.             UIImageOrientation originalOrientation = [[editInfo objectForKey:UIImagePickerControllerOriginalImage] imageOrientation];
  40.             if (originalOrientation != UIImageOrientationUp) {
  41.                 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  42.                 CGRect origRect;
  43.                 [[editInfo objectForKey:UIImagePickerControllerCropRect] getValue:&origRect];
  44.                 UIImage *rotatedImage = straightenAndScaleImage([editInfo objectForKey:UIImagePickerControllerOriginalImage], scaleSize);
  45.                 CGFloat scale = scaleSize/640.0f;
  46.                 origRect.origin.x *= scale;
  47.                 origRect.origin.y *= scale;
  48.                 origRect.size.width *= scale;
  49.                 origRect.size.height *= scale;
  50.                 croppedImage = [self cropImage:rotatedImage to:origRect andScaleTo:CGSizeMake(320, 480)];
  51.                 [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
  52.                 [pool drain];
  53.             }
  54.             else {
  55.                 croppedImage = [self scaleImage:img withEditingInfo:editInfo];
  56.                 [imageDescriptor setObject:croppedImage forKey:@"croppedImage"];
  57.             }
  58.         }
  59.             break;
  60.         default:
  61.             break;
  62.     }
  63.     imageFrame.size = croppedImage.size;
  64.     myImageView.frame = imageFrame;
  65.     myImageView.image = [imageDescriptor objectForKey:@"croppedImage"];
  66.     myImageView.center = imageCenter;
  67. }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
1段“四女侍一夫”视频曝光,引全网热议:咋这么疯?
世界遗产名录
演员霍思燕:我这辈子最正确的决定,就是在33岁为丈夫生儿子
猪蹄炖不烂,只要加上这5种香料,保证猪蹄软糯脱骨,满口留香
超详细的结构分析
1味中药虎虎生威!把肝胆肠胃里的淤堵,全都清理掉
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服