2011-07-04 84 views
1

我在我的应用程序中使用UIImagePickerController拍照,我使用我自己的控件,这意味着UIImagePickerController showsCameraControls属性设置为NO,并且在拍摄图片的overlayView中有一个UIButton。现在,我注意到我保存在照片库中的图像实际上显示的区域比预览视图中显示的区域大。其他人有同样的问题吗?任何解决方案,让图片显示预览中的内容?UIImagePickerController图像大小

回答

1

通过预览我想,你是在谈论影像选择器接口(而不是默认的预览驳回图像拾取界面后出现屏幕)。

您应用于图像选择器界面(使用cameraViewTransform)的变换无法反映所拍摄的图像。例如,如果您试图缩放(进出)应用比例,则需要对获取的图像应用相同的(变换),以使图像拾取器界面中的图像和实际保存的图像保持同步。
此外,在应用转换时,您还必须考虑图像方向。

2

从选择器获取图像对象后,调整图像,然后裁剪

我需要同样的事情 - 在我的情况,挑选适合一旦缩放尺寸,然后裁剪每端,以适应其余的宽度。 (我在横向工作,所以可能没有注意到纵向模式中的任何缺陷。)这里是我的代码 - 它是UIImage上的一个关于categeory的部分。我的代码中的目标大小始终设置为设备的全屏大小。

@implementation UIImage (Extras) 

#pragma mark - 
#pragma mark Scale and crop image 

- (UIImage*)imageByScalingAndCroppingForSize:(CGSize)targetSize 
{ 
UIImage *sourceImage = self; 
UIImage *newImage = nil;  
CGSize imageSize = sourceImage.size; 
CGFloat width = imageSize.width; 
CGFloat height = imageSize.height; 
CGFloat targetWidth = targetSize.width; 
CGFloat targetHeight = targetSize.height; 
CGFloat scaleFactor = 0.0; 
CGFloat scaledWidth = targetWidth; 
CGFloat scaledHeight = targetHeight; 
CGPoint thumbnailPoint = CGPointMake(0.0,0.0); 

if (CGSizeEqualToSize(imageSize, targetSize) == NO) 
    { 
    CGFloat widthFactor = targetWidth/width; 
    CGFloat heightFactor = targetHeight/height; 

    if (widthFactor > heightFactor) 
     scaleFactor = widthFactor; // scale to fit height 
    else 
     scaleFactor = heightFactor; // scale to fit width 
    scaledWidth = width * scaleFactor; 
    scaledHeight = height * scaleFactor; 

    // center the image 
    if (widthFactor > heightFactor) 
     { 
     thumbnailPoint.y = (targetHeight - scaledHeight) * 0.5; 
     } 
    else 
     if (widthFactor < heightFactor) 
      { 
      thumbnailPoint.x = (targetWidth - scaledWidth) * 0.5; 
      } 
    } 

UIGraphicsBeginImageContext(targetSize); // this will crop 

CGRect thumbnailRect = CGRectZero; 
thumbnailRect.origin = thumbnailPoint; 
thumbnailRect.size.width = scaledWidth; 
thumbnailRect.size.height = scaledHeight; 

[sourceImage drawInRect:thumbnailRect]; 

newImage = UIGraphicsGetImageFromCurrentImageContext(); 
if(newImage == nil) 
    NSLog(@"could not scale image"); 

//pop the context to get back to the default 
UIGraphicsEndImageContext(); 
return newImage; 
} 
+0

谢谢您的回复。这不是我真正需要的。我注意到对cameraViewTransform应用一个缩放转换,那么你将能够看到比你实际保存到图书馆更小的区域。 – singingAtom