动态地剪裁景观图像

时间:2012-11-30 07:27:31

标签: iphone ios image-processing crop

我想根据红色视图裁剪图像。要记住一些要点。

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

 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:正常(未滚动)

enter image description here

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

场景2:滚动

enter image description here

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

enter image description here

场景3:放大

enter image description here

与Zoomed One相同的预期结果。

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

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

    -(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);
    }

2 个答案:

答案 0 :(得分:1)

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

//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加倍的原因。

答案 1 :(得分:0)

您可以使用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];