注册 登录
主题 : UIImagePickerController编辑图片框大小的问题
级别: 圣骑士

状态: 连续签到 - [3天]
UID: 29429
精华: 0
发帖: 383
可可豆: 2990 CB
威望: 2933 点
在线时间: 575(时)
注册时间: 2010-09-02
最后登录: 2018-08-13
0 楼:  发表于: 2010-09-21 14:41    发自: Web Page
来源于 一般提问 分类

UIImagePickerController编辑图片框大小的问题   

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













关键词: 图片 Controller
级别: 侠客
UID: 16507
精华: 0
发帖: 85
可可豆: 850 CB
威望: 850 点
在线时间: 95(时)
注册时间: 2010-03-31
最后登录: 2018-06-05
1 楼:  发表于: 2010-09-21 18:07    发自: Web Page
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. }

Email: cary_gao@hotmail.com
级别: 侠客
状态: 连续签到 - [1天]
UID: 37772
精华: 0
发帖: 118
可可豆: 1077 CB
威望: 1077 点
在线时间: 397(时)
注册时间: 2010-11-11
最后登录: 2018-09-04
2 楼:  发表于: 2011-05-05 21:03    发自: Web Page
留下来明天好好看看
级别: 骑士
UID: 13329
精华: 0
发帖: 213
可可豆: 1702 CB
威望: 1702 点
在线时间: 527(时)
注册时间: 2010-01-23
最后登录: 2017-10-24
3 楼:  发表于: 2011-06-09 10:42    发自: Web Page
收藏,貌似需要用到
级别: 圣骑士

UID: 50373
精华: 0
发帖: 465
可可豆: 3579 CB
威望: 3579 点
在线时间: 651(时)
注册时间: 2011-02-11
最后登录: 2017-07-24
4 楼:  发表于: 2011-09-20 20:36    发自: Web Page
mark  。。。。。。
什么时候才能成精灵王呀
级别: *
UID: 90412
精华: *
发帖: *
可可豆: * CB
威望: * 点
在线时间: (时)
注册时间: *
最后登录: *
5 楼:  发表于: 2011-09-21 09:38    发自: Web Page
废话不多说,必须好好学。。。
级别: 圣骑士
UID: 42460
精华: 0
发帖: 359
可可豆: 2962 CB
威望: 2963 点
在线时间: 519(时)
注册时间: 2010-12-13
最后登录: 2018-12-27
6 楼:  发表于: 2011-10-03 17:57    发自: Web Page
留下, 以后好好用,谢谢楼主 无私奉献
级别: 侠客
UID: 84666
精华: 0
发帖: 128
可可豆: 1167 CB
威望: 1167 点
在线时间: 181(时)
注册时间: 2011-08-02
最后登录: 2013-05-06
7 楼:  发表于: 2011-10-08 16:26    发自: Web Page
不知是否有用?mark先,后续有用到再试试
级别: 圣骑士

UID: 53328
精华: 2
发帖: 484
可可豆: 3437 CB
威望: 3422 点
在线时间: 670(时)
注册时间: 2011-02-25
最后登录: 2019-01-16
8 楼:  发表于: 2011-10-09 09:44    发自: Web Page
楼主的可以将UIImageView的图片适应设置下,那不就OK了,让他自动适应!!!!
哥不说话,哥不想暴露哥那山区的浑厚重低音.
哥不说话,哥用山寨iphone拍下哥凌乱沧凉的白发.
哥不说话,哥轻轻抚摸着额头,怅然的回忆着哥5岁时那破碎的忧伤.
哥不说话,哥只是用完美的角度翘起了嘴角.
级别: 新手上路
UID: 30038
精华: 0
发帖: 3
可可豆: 30 CB
威望: 30 点
在线时间: 101(时)
注册时间: 2010-09-08
最后登录: 2016-03-27
9 楼:  发表于: 2011-10-09 16:56    发自: Web Page
mark先.留下, 以后好好用,谢谢楼主 无私奉献

CocoaChina社区转载内容已尽可能注明出处,如未能核实来源或转发内容图片有权利瑕疵的,请及时联系社区进行修改或删除【联系方式QQ : 3442093904 邮箱:support@cocoachina.com】文章内容为作者独立观点,不代表CocoaChina社区立场。版权归原作者所有,如申请授权请联系作者,因文章侵权CocoaChina社区不承担任何法律及连带责任。

描述
快速回复

关注本帖(如果有新回复会站内信通知您)

发帖、回帖都会得到可观的积分奖励。查看论坛积分规则

按"Ctrl+Enter"直接提交
    顶部