ios - 在 UIImageView 中显示之前裁剪图像

标签 ios uiimageview crop

我是我的应用程序,我需要裁剪和从互联网下载的图像。我使用这种方法下载图像:

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

                           }];
}

我如何裁剪它?

最佳答案

定义一个矩形,这个矩形将是你的图像的裁剪区域。

CGRect croprect  = CGRectMake(x,y,width, height);

CGImageRef subImage = CGImageCreateWithImageInRect (yourimage,croprect);

这里我们使用 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.

最后生成图片,

UIImage *newImage = [UIImage imageWithCGImage:subImage];

关于ios - 在 UIImageView 中显示之前裁剪图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17784922/

相关文章:

ios - 无效的预发布训练。火车版本 '1.0' 已关闭,无法提交新版本

iphone - 导航 plist 和设置对象

ios 快速解析 : How to handle error codes

iOS Push UIViewController 像 Snapchat 这样的滑动手势

ios - 将一张图像分成多个部分后,如何在 Swift 中使用 UIImage 数组?

php - 我如何在 php 中使用 imagick? (调整大小和裁剪)

neural-network - 裁剪/缩放 ImageNet 图像

ios - 我怎样才能看到 UIImageView 中的图像?

ios - 将 FBProfilePictureView 转为 UIImage

javascript - Fabric.js 对象内的裁剪路径