【问题标题】:Crop Landscape Image Dynamically动态裁剪风景图像
【发布时间】:2012-11-18 09:50:53
【问题描述】:

我想根据红色 View 裁剪 Image 。有几点需要牢记。

1.图像可以滚动和缩放。 2.Red ImageView是根据Image动态创建的

 UIImage* whole = [UIImage imageNamed:@"9.png"]; //I uses this image
 CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, incX, incY));
 UIImage* part = [UIImage imageWithCGImage:cgImg];

我只想知道如何找到

的值

x, y, incX, incY

谢谢...

场景 1:正常(不滚动)

预期结果(忽略顶部和底部的黑色边框)

场景2:滚动

预期结果(忽略顶部和底部的黑色边框)

场景 3:放大

缩放后的预期结果相同。

在所有情况下,我都希望红色矩形内的相应图像。

对于所有这些,我正在使用此代码...

    -(void)cropClicked:(UIButton*)sender
    {
        float zoomScale = 1.0 / [mainScrollView zoomScale];
        CGRect rect;
        rect.size.width = [redImageView bounds].size.width * zoomScale ;
        rect.size.height = [redImageView bounds].size.height * zoomScale ;
        rect.origin.x = ([mainScrollView bounds].origin.x + redImageView.frame.origin.x );
        rect.origin.y = ([mainScrollView bounds].origin.y + redImageView.frame.origin.y );

        CGImageRef cr = CGImageCreateWithImageInRect([[mainImageView image] CGImage], rect);
        UIImage *cropped = [UIImage imageWithCGImage:cr];
        mainImageView.image=cropped;
        UIImageWriteToSavedPhotosAlbum(cropped, nil, nil, nil);
        CGImageRelease(cr);
    }

【问题讨论】:

  • 你想让用户画那个矩形吗?
  • No @HDdeveloper 我想要 redImage 的 X 和 Y 出现在屏幕上的点,无论是滚动还是缩放。

标签: iphone ios image-processing crop


【解决方案1】:

好吧,正如@HDdeveloper 所说,您可以使用CGImageCreateWithImageInRect。这需要 2 个参数,第一个是整个图像,第二个是您要裁剪的帧(所以可能是您的红色 imageView 的帧)。
问题是,如果您同时针对视网膜/非视网膜;如果您的整个图像是 @2x 的图像,并且您想用红色的 imageview 框架裁剪图像,则必须将您的框架加倍才能获得正确的屏幕截图。
所以你可以试试这个方法:

//Define the screen type:
#define isRetinaDisplay [[UIScreen mainScreen] respondsToSelector:@selector(displayLinkWithTarget:selector:)] && ([UIScreen mainScreen].scale == 2.0)

- (UIImage*)cropInnerImage:(CGRect)rect {
    //Take a screenshot of the whole image
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage* ret = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    CGRect rct;
    //Double the frame if you're in retina display
    if (isRetinaDisplay) {
        rct=CGRectMake(rect.frame.origin.x*2, rect.frame.origin.y*2, rect.size.width*2, rect.size.height*2);
    } else {
        rct=rect;
    }
    //Crop the image from the screenshot
    CGImageRef imageRef = CGImageCreateWithImageInRect([ret CGImage], rct);
    UIImage *result = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    //Save and open the result images with Preview.app
    [UIImagePNGRepresentation(result) writeToFile: @"/tmp/testCrop.png" atomically: YES];
    system("open /tmp/testCrop.png");
    [UIImagePNGRepresentation(ret) writeToFile: @"/tmp/testRet.png" atomically: YES];
    system("open /tmp/testRet.png");
    //
    return result;
}

其中rect 参数必须是您的红色图像帧,self.view.frame 必须等于wholeImageView.frame。您可以跳过最后 4 行,这些只是为了在您的 Mac 中查看您正在裁剪的内容。

PS:我使用这种方法裁剪图像并将其设置为 UIView 的背景,这就是我必须将frame 加倍的原因。

【讨论】:

  • 非常感谢@Mat,但我如何计算传递给这个方法的不是redImage的Rect
  • 如果我通过 redImage Rect 然后图像将始终用 redImage 的 x 和 y 裁剪,正如我已经提到的,我的图像也可以被缩放和缩放
  • 所以我正在寻找矩形的确切 X 和 Y 高度和宽度可以从 ScrollView 的缩放比例得出
  • 所以总的来说,我希望 redImage 的 X 和 Y 出现在屏幕上,无论是滚动还是缩放。
  • 用户可以缩放的图像是什么?整张图像还是裁剪后的红色图像?无论如何,如果您将红色的 imageView 放在屏幕上,那么您肯定还有帧的 x,y,您可以使用 CGRect ivar 来跟踪设置为 imageView 的最后一帧并将其传递给方法.
【解决方案2】:

你可以使用 CGImageRef 在整个图像中传递你的矩形。然后在按钮单击时调用它

UIImage* whole = [UIImage imageNamed:@"9.png"]; //I uses this image
    CGImageRef cgImg = CGImageCreateWithImageInRect(whole.CGImage, CGRectMake(x, y, incX, incY));
    UIImage* part = [UIImage imageWithCGImage:cgImg];

【讨论】:

  • 感谢您的关注,但我想知道您如何找到 x、y、incX、incY
  • @Anand,您可以使用图像缩放比例来确定。
猜你喜欢
  • 2014-07-16
  • 1970-01-01
  • 2022-01-23
  • 1970-01-01
  • 2017-05-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多