动态裁剪风景图像 [英] Crop Landscape Image Dynamically

查看:91
本文介绍了动态裁剪风景图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要根据红色视图裁剪图像。需要牢记一些重点。

I want to crop Image according to red View . There are some points to keep in mind.

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

1.Image can be scrolled and Zoomed. 2.Red ImageView is created Dynamically according to 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

x, y, incX, incY

谢谢...

场景1:正常(未滚动)

Scenario 1: Normal (Not Scrolled)

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

Expected Result (Ignore Black Border On Top and Bottom)

方案2:滚动

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

Expected Result (Ignore Black Border On Top and Bottom)

场景3:缩放

与缩放对象的预期结果相同。

And same Expected Result for the Zoomed One.

l情况下,我想在红色矩形内放置相应的图像。

In all cases I want the respective Images Inside the Red Rectangle.

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

For all These I am Using this Code...

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


推荐答案

好吧,就像@HDdeveloper说,您可以使用 CGImageCreateWithImageInRect 。这需要2个参数,第一个是整个图像,第二个是您要裁剪的框架(因此可能是红色imageView的框架)。

问题是,如果您要定位视网膜/非视网膜如果您的整个图像都是@ 2x图像,并且您想使用红色的imageview框架裁剪图像,则必须将框架加倍以获取正确的屏幕截图。

因此,您可以尝试以下方法:

Well, as @HDdeveloper rightly said, you can use CGImageCreateWithImageInRect. This take 2 params, the first is the whole image, the second is the frame that you want to crop (so probably the frame of your red imageView).
The problem is that if you're targeting for both retina/non retina; if your whole image is an image @2x and you want to crop the image with the red imageview frame you have to double your frame to get the right screenshot.
So you can try with this method:

//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中查看您要裁剪的内容。

Where the rect parameter must be your red image frame, and self.view.frame must be the equal to the wholeImageView.frame. You can skip the last 4 lines, these are just to see in your Mac what you're cropping.

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

PS: i use this method to crop an image and set it as background of UIView, this is the reason i have to double the frame.

这篇关于动态裁剪风景图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆