ios - CGImageCreateWithImageInRect 区域不正确?

标签 ios screenshot area cgimage

我正在使用这个 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];
}

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

谢谢!

最佳答案

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

关于ios - CGImageCreateWithImageInRect 区域不正确?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10473927/

相关文章:

ios - 从 xcode Storyboard中可视化修改 UIToolbar

java - 将屏幕截图附加到 TestNG 失败方法结果

java - 多边形面积

ios - 无法删除被拒绝的 IAP

ios - 如何更改位于 UIPageViewController 的 ViewController 中的 label.text?快速 ios xcode

android - Phonegap VS 类固醇。我选对了吗?

java - 使用 MediaProjection API 截取屏幕截图时的额外填充

objective-c - 以编程方式截取特定区域的屏幕截图

javascript - 我将如何实现鞋带定理来找到由相交线创建的多个凸多边形的面积?

c - 如何在有两个圆圈的矩形中找到空白区域?