如何在“方面适合"模式下将 UIImageView 裁剪为新的 UIImage? [英] How to crop a UIImageView to a new UIImage in 'aspect fit' mode?

查看:11
本文介绍了如何在“方面适合"模式下将 UIImageView 裁剪为新的 UIImage?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过在 UIImageView 中裁剪图像来创建一个新的 UIImage.例如在上面的图片中,我想要一个以绿色勾勒出的区域的新 UIImage.我找到了执行此操作的代码,通常它看起来像这样(作为 UIImage 类别):

I would like to create a new UIImage by cropping the image inside a UIImageView. For example in the above pic, I want a new UIImage of the area outlined in green. I found code to do this, normally it looks something like this (as a UIImage category):

- (UIImage *)croppedImage:(CGRect)bounds {
    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
    UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
    CGImageRelease(imageRef);
    return croppedImage;
}

非常有道理.但是,我的 UIImageView 处于 contentMode 'aspect fit' 中.似乎这使 CGImageCreateWithImageInRect 的 CGRect 计算复杂化,我一直无法找出正确的解决方案.

Makes perfect sense. HOWEVER, my UIImageView is in contentMode 'aspect fit'. Seems like that complicates the CGRect calculations for CGImageCreateWithImageInRect and I haven't been able to figure out the right solution.

我想我需要对绿色矩形应用与对图像所做的相同的转换吗?

I figure I need to apply the same transformations to the green rect as were done to the image?

另外:我在这里找到了另一个问题(如何在 UIImageView 中应用适合图像的方面后知道图像大小),这似乎显示了一种获取大小的蛮力方式,但我是仍然不确定这如何适应种植情况.

ALSO: I found this other question here (How to know the image size after applying aspect fit for the image in an UIImageView), which seems to show a brute force way of getting the size, but I am still not sure how that fits into a cropping situation.

有什么想法吗?

编辑:在我的例子中,我确实在 UIImageView 上的覆盖视图中绘制了一个如上所示的正方形.因此,当我意识到我正在裁剪 UIImage(在 UIImageView 内)时,我需要以某种方式更改绿色 CGRect,使其匹配"应用方面适合"时完成的转换.例如,如果我将 UIImageView 置于左上"模式,那么一切正常,因为底层 UIImage 和 UIImageView 之间存在 1:1 映射.左上"模式的问题是整个图像并不总是显示,因为它可能比我的 UIImageView 大.

EDIT: In my case, I literally have a square as shown above drawn in an overlay view over the UIImageView. So while I realize I am cropping the UIImage (inside the UIImageView), I need to somehow change the green CGRect such that it 'matches' the transformation done when 'aspect fit' is applied. For example, if I leave my UIImageView in 'top left' mode, then everything works fine, because there is a 1:1 mapping between the underlying UIImage and the UIImageView. The problem in 'top left' mode is that the entire image doesn't always display, because it might be bigger than my UIImageView.

推荐答案

基于我对类似问题的其他回答我已经为 UIImageView 类别编写了这个方法:

Based on my other answer to a similar question I have written this method for a UIImageView category:

-(CGRect) cropRectForFrame:(CGRect)frame
{
    NSAssert(self.contentMode == UIViewContentModeScaleAspectFit, @"content mode must be aspect fit");

    CGFloat widthScale = self.bounds.size.width / self.image.size.width;
    CGFloat heightScale = self.bounds.size.height / self.image.size.height;

    float x, y, w, h, offset;
    if (widthScale<heightScale) {
        offset = (self.bounds.size.height - (self.image.size.height*widthScale))/2;
        x = frame.origin.x / widthScale;
        y = (frame.origin.y-offset) / widthScale;
        w = frame.size.width / widthScale;
        h = frame.size.height / widthScale;
    } else {
        offset = (self.bounds.size.width - (self.image.size.width*heightScale))/2;
        x = (frame.origin.x-offset) / heightScale;
        y = frame.origin.y / heightScale;
        w = frame.size.width / heightScale;
        h = frame.size.height / heightScale;
    }
    return CGRectMake(x, y, w, h);
}

您可以传入绿色矩形的框架(假设它是图像视图的子视图)并获取裁剪矩形以裁剪 UIImage.请注意,它仅适用于方面适合"模式!不过我还没有测试过.所以,告诉我它是否有效!

You can pass in the frame of your green rect (assuming it is a subview of the image view) and get the crop rect back for cropping the UIImage. Note that it only works for 'aspect fit' mode! I haven't tested it though. So, tell me if it works!

这篇关于如何在“方面适合"模式下将 UIImageView 裁剪为新的 UIImage?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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