簡體   English   中英

在UIImageView中顯示之前裁剪圖像

[英]Crop image before show in a UIImageView

我是我的應用程序,需要裁剪和下載從互聯網下載的圖像。 我使用這種方法下載圖像:

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

多個庫可以為您完成此任務。 您可以選擇一個並使用它,也可以研究一對並了解它是如何完成的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM