【问题标题】:CGImageCreateWithImageInRect incorrect area?CGImageCreateWithImageInRect 区域不正确?
【发布时间】:2012-05-06 20:18:06
【问题描述】:

我正在使用这个 github 图片裁剪器:https://github.com/iosdeveloper/ImageCropper

在这个图像裁剪器中,在 init 方法中它们有以下几行:

CGRect rect;
        rect.size.width = image.size.width;
        rect.size.height = image.size.height;

        [imageView setFrame:rect];

事先,我将图像调整为屏幕分辨率而不是点。因此,如果我将代码保持原样,我的 UIImage 将不会因为某些奇怪的原因填充 UIImageView,我也尝试调整 contentModes。

但是如果我将它调整到这个(图像裁剪器所在的 UIPopover 的大小):

[imageView setFrame:CGRectMake(0, 0, 320, 480)];

UIImage 填充 UIPopover。

真正的问题在这里: 如果我放大图像的某个部分并执行下面的代码,它将截取 UIImage 的左上角部分,而不是靠近我裁剪图像的位置。虽然它确实遵守了scrollView的zoomScale。

这里是截图代码:

- (void)finishCropping {

    float zoomScale = 1.0 / [scrollView zoomScale];

    CGRect rect;
    rect.origin.x = [scrollView contentOffset].x * zoomScale;
    rect.origin.y = [scrollView contentOffset].y * zoomScale;
    rect.size.width = [scrollView bounds].size.width * zoomScale;
    rect.size.height = [scrollView bounds].size.height * zoomScale;

    CGImageRef cr = CGImageCreateWithImageInRect([[imageView image] CGImage], rect);

    UIImage *cropped = [UIImage imageWithCGImage:cr];

    CGImageRelease(cr);

    [delegate imageCropper:self didFinishCroppingWithImage:cropped];
}

有人知道为什么会这样吗?

谢谢!

【问题讨论】:

    标签: ios screenshot area cgimage


    【解决方案1】:

    我认为您首先必须使用[UIImage imageWithCGImage:cr scale:self.scale orientation:self.imageOrientation] 而不是[UIImage imageWithCGImage:cr] 创建UIImage,这将消除许多方向问题并仔细检查矩形的宽度和高度是否正确。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-30
      • 1970-01-01
      • 1970-01-01
      • 2013-11-14
      • 2011-05-18
      • 2018-08-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多