【问题标题】:How to do properly cropping of UIImage taken with UIImagePickerController?如何正确裁剪使用 UIImagePickerController 拍摄的 UIImage?
【发布时间】:2014-08-23 09:33:56
【问题描述】:

这是我的应用的相机叠加层,

黄色方块表示用户只保存此部分(相机中)内的照片。就像庄稼一样。

当我保存捕获的图像时,它会保存放大的照片[放大的大照片],

我发现,当我拍照时,它的大小是{2448, 3264}

我正在像这样裁剪图像,

- (UIImage *)imageByCroppingImage:(UIImage *)image toSize:(CGSize)size
{
    double x = (image.size.width - size.width) / 2.0;
    double y = (image.size.height - size.height) / 2.0;

    CGRect cropRect = CGRectMake(x, y, size.height, size.width);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], cropRect);

    UIImage *cropped = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);

    return cropped;
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];

    if (image) {
        UIImage *newImage = [self imageByCroppingImage:image toSize:CGSizeMake(300.f, 300.f)];
        UIImageWriteToSavedPhotosAlbum(newImage, nil, nil, nil);
    }
}

注意事项,

  • 在执行裁剪之前已修复方向。使用这个,http://pastebin.com/WYUkDLS0

  • 相机上的那个黄色方块也是同样大小,宽度=300,高度=300。

  • 如果我将前置摄像头设置为UIImagePickerController,那么它会给我完美的裁剪图像输出。 是的,这真的很奇怪!

  • 我已经尝试了这里的一切,Cropping an UIImage。即使https://github.com/Nyx0uf/NYXImagesKit 也无济于事。

有什么想法/建议吗?


更新:

从这个问题,Trying to crop my UIImage to a 1:1 aspect ratio (square) but it keeps enlarging the image causing it to be blurry. Why?

我是这样跟着@DrummerB 的回答的,

    CGFloat originalWidth = image.size.width * image.scale;
    CGFloat originalHeight = image.size.height * image.scale;
    float smallestDimension = fminf(originalWidth, originalHeight);
    CGRect square = CGRectMake(0, 0, smallestDimension, smallestDimension);
    CGImageRef imageRef = CGImageCreateWithImageInRect([image CGImage], square);
    UIImage *squareImage = [UIImage imageWithCGImage:imageRef scale:image.scale orientation:image.imageOrientation];
    UIImageWriteToSavedPhotosAlbum(squareImage, nil, nil, nil);
    CGImageRelease(imageRef);

这是我拍的,

结果如下,

现在我正在获取方形照片,但在输出中请注意,我仍然在那个黄色正方形之外获取照片。我想要的是获得位于黄色方块中的照片。捕获的图像仍然是大小,{w=2448, h=3264}。请注意,红色圆圈表示图像的外部部分不应包含在输出中,因为该部分不在黄色方块内。

这有什么问题?

【问题讨论】:

    标签: ios uiimage uiimagepickercontroller crop cameraoverlayview


    【解决方案1】:

    看起来您在实施中收到的图像正在返回 300 x 300 像素的图像裁剪。屏幕上的黄色方块是 300 x 300 点。点与像素不同。因此,如果您的照片宽为 3264 像素,则将其裁剪为 300 像素将返回大约 1/10 原始尺寸的图像。

    【讨论】:

    • 你需要使用数学的力量。 square.size 是 screen.size,因为 ______ 是 picture.sizeInPixels。
    猜你喜欢
    • 2020-11-19
    • 2013-09-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多