2013-07-22 220 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