UIImagePickerController编辑图片框大小的问题

解决UIImageView显示UIImagePickerController裁剪图片时的尺寸不匹配问题,通过调整裁剪框大小以适应不同比例的目标视图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源于  一般提问 分类

UIImagePickerController编辑图片框大小的问题   

本人照片的ImageView大小为120*145,而UIImagePickerController的图片编辑框为320*320,如图3白线部分,这样编辑出来的图片大小应该是正方形的,所以放到ImageView里会有问题,请问各位大大,这种情况怎么解决?我不想把ImageView的大小改成方的,能否修改编辑框的大小?











  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. }



  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. }

转载自:http://www.cocoachina.com/bbs/read.php?tid=33022
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值