【问题标题】:UIImagePickerController image sizeUIImagePickerController 图像大小
【发布时间】:2011-07-04 14:48:52
【问题描述】:

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

【问题讨论】:

    标签: iphone uiimagepickercontroller


    【解决方案1】:

    从选取器获取图像对象后调整图像大小然后裁剪

    我需要同样的东西 - 在我的例子中,选择适合缩放后的尺寸,然后裁剪每一端以适应其余部分的宽度。 (我在横向工作,所以可能没有注意到纵向模式的任何缺陷。)这是我的代码 - 它是 UIImage 类别的一部分。我的代码中的目标尺寸始终设置为设备的全屏尺寸。

    @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;
    }
    

    【讨论】:

    • 感谢您的回复。不过,这并不是我真正需要的。我注意到对 cameraViewTransform 应用缩放变换后,您将能够看到比实际保存到库中的区域更小的区域。
    【解决方案2】:

    我假设通过预览,您正在谈论图像选择器界面(而不是关闭图像选择器界面后出现的默认预览屏幕)。

    应用于图像选择器界面的变换(使用 cameraViewTransform)不会反映在拍摄的图像上。例如,如果您尝试缩放(放大和缩小)应用比例,则需要在获得的图像上应用相同(变换)以使图像选择器界面中的图像与实际保存的图像保持同步。
    此外,在应用变换时,您还必须考虑图像方向。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多