裁剪 UIImage [英] Cropping an UIImage
问题描述
我有一些代码可以调整图像大小,因此我可以获得图像中心的缩放块 - 我使用它来获取 UIImage
并返回一个小的方形表示图像,类似于在照片应用程序的相册视图中看到的内容.(我知道我可以使用 UIImageView
并调整裁剪模式以获得相同的结果,但这些图像有时会显示在 UIWebViews
中).
我开始注意到这段代码中有一些崩溃,我有点难住了.我有两种不同的理论,我想知道是否有一种是基于基础的.
理论 1) 我通过绘制目标尺寸的屏幕外图像上下文来实现裁剪.由于我想要图像的中心部分,因此我将传递给 drawInRect
的 CGRect
参数设置为大于图像上下文边界的值.我希望这是 Kosher,但我是不是试图勾勒出我不应该触及的其他记忆?
理论 2) 我在后台线程中执行所有这些操作.我知道 UIKit 的某些部分仅限于主线程.我假设/希望绘制到屏幕外视图不是其中之一.我错了吗?
(哦,我多么想念 NSImage 的 drawInRect:fromRect:operation:fraction:
方法.)
更新 2014-05-28:当 iOS 3 左右是热门新事物时,我写了这个,我确信有更好的方法来做到这一点现在,可能是内置的.正如很多人提到的,这种方法没有考虑轮换;阅读一些其他答案并传播一些赞誉,以使对这个问题的回答对每个人都有帮助.
原回复:
我要将我对同一问题的回答复制/粘贴到别处:
没有一个简单的类方法可以做到这一点,但有一个函数可以用来获得所需的结果:CGImageCreateWithImageInRect(CGImageRef, CGRect)
会帮助你.>
这是一个使用它的简短示例:
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage],cropRect);//或者在任何你喜欢的地方使用 UIImage[UIImageView setImage:[UIImage imageWithCGImage:imageRef]];CGImageRelease(imageRef);
I've got some code that resizes an image so I can get a scaled chunk of the center of the image - I use this to take a UIImage
and return a small, square representation of an image, similar to what's seen in the album view of the Photos app. (I know I could use a UIImageView
and adjust the crop mode to achieve the same results, but these images are sometimes displayed in UIWebViews
).
I've started to notice some crashes in this code and I'm a bit stumped. I've got two different theories and I'm wondering if either is on-base.
Theory 1) I achieve the cropping by drawing into an offscreen image context of my target size. Since I want the center portion of the image, I set the CGRect
argument passed to drawInRect
to something that's larger than the bounds of my image context. I was hoping this was Kosher, but am I instead attempting to draw over other memory that I shouldn't be touching?
Theory 2) I'm doing all of this in a background thread. I know there are portions of UIKit that are restricted to the main thread. I was assuming / hoping that drawing to an offscreen view wasn't one of these. Am I wrong?
(Oh, how I miss NSImage's drawInRect:fromRect:operation:fraction:
method.)
Update 2014-05-28: I wrote this when iOS 3 or so was the hot new thing, I'm certain there are better ways to do this by now, possibly built-in. As many people have mentioned, this method doesn't take rotation into account; read some additional answers and spread some upvote love around to keep the responses to this question helpful for everyone.
Original response:
I'm going to copy/paste my response to the same question elsewhere:
There isn't a simple class method to do this, but there is a function that you can use to get the desired results: CGImageCreateWithImageInRect(CGImageRef, CGRect)
will help you out.
Here's a short example using it:
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
// or use the UIImage wherever you like
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]];
CGImageRelease(imageRef);
这篇关于裁剪 UIImage的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!