[英]Crop image before show in a UIImageView
I'm my app I need to crop and image downloaded from internet. 我是我的应用程序,需要裁剪和下载从互联网下载的图像。 I download the image using this method: 我使用这种方法下载图像:
- (void) loadImageFromWeb {
NSURL* url = [NSURL URLWithString:self.imageURL];
NSURLRequest* request = [NSURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,
NSData * data,
NSError * error) {
if (!error){
UIImage* image = [[UIImage alloc] initWithData:data];
[self.imageViewEpisode setImage:image];
}
}];
}
How I can crop it? 我如何种植它?
Define a rect, this rect will be the crop area of your image. 定义一个矩形,此矩形将是图像的裁剪区域。
CGRect croprect = CGRectMake(x,y,width, height);
CGImageRef subImage = CGImageCreateWithImageInRect (yourimage,croprect);
Here we have used CoreGraphics to create a sub image from your image. 在这里,我们使用CoreGraphics从您的图像创建子图像。
Creates a bitmap image using the data contained within a subregion of an existing bitmap image.
CGImageRef CGImageCreateWithImageInRect (
CGImageRef image,
CGRect rect
);
Parameters
image
The image to extract the subimage from.
rect
A rectangle whose coordinates specify the area to create an image from.
Return Value
A CGImage object that specifies a subimage of the image. If the rect parameter defines an area that is not in the image, returns NULL.
Finally generate image, 最终生成图像,
UIImage *newImage = [UIImage imageWithCGImage:subImage]; UIImage * newImage = [UIImage imageWithCGImage:subImage];
There are multiple libraries that can do that for you. 有多个库可以为您完成此任务。 You can either pick one and work with it, or study a couple and understand how it's done. 您可以选择一个并使用它,也可以研究一对并了解它是如何完成的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.