2013-07-22 214 views
1

我是我的應用程序,我需要裁剪和從互聯網上下載圖像。我下載使用這種方法的圖像:在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]; 
           } 

          }]; 
} 

我如何裁剪?

回答

1

定義一個矩形,這個矩形將是你的圖像的裁剪區域。

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:子圖像]。

+0

我也會嘗試這個解決方案。謝謝! – lucgian84

0

multiple libraries可以爲你做到這一點。你可以選擇一個並使用它,或者學習一對夫婦並理解它是如何完成的。

+0

好的,謝謝你我會看看這個圖書館! – lucgian84