2014-03-29 41 views
0

裁剪圖像,並設置爲另一個視圖如何修剪出特定位置的圖像並設定爲從特定位置的另一視圖

最終圖像視圖

self.finalImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 320, 320)]; 
if(rectangle_button_preesed_view) 
{ 
    self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 120, 260, 340)]; 
} 
else 
{ 
    self.finalImageView.image =[self croppIngimageByImageName:self.imageView.image toRect:CGRectMake(30, 80, 260, 260)]; 
} 

裁剪圖像

- (UIImage *)croppIngimageByImageName:(UIImage *)imageToCrop toRect:(CGRect)rect 
{ 
    CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect); 
    UIImage *cropped = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 
    NSLog(@" cropped size %f %f ",cropped.size.width,cropped.size.height); 
    return cropped;  
} 

回答

0
@implementation UIImage (Crop) 

- (UIImage *)crop:(CGRect)cropRect { 

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], cropRect); 
    UIImage *cropedImage = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef); 

    return cropedImage; 

} 
0
CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect); 
// or use the UIImage wherever you like 
[UIImageView setImage:[UIImage imageWithCGImage:imageRef]]; 
CGImageRelease(imageRef); 

OR

@implementation UIImage (Crop) 

- (UIImage *)crop:(CGRect)rect { 

    rect = CGRectMake(rect.origin.x*self.scale, 
         rect.origin.y*self.scale, 
         rect.size.width*self.scale, 
         rect.size.height*self.scale);  

    CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], rect); 
    UIImage *result = [UIImage imageWithCGImage:imageRef 
              scale:self.scale 
            orientation:self.imageOrientation]; 
    CGImageRelease(imageRef); 
    return result; 
} 

@end 
0

除了CoreGraphics的解決方案,我也建議使用CoreImage支持的圖像處理過濾器。您應該查看this documentation page中的CICrop篩選器。

注意:此過濾器支持iOS 5或更高版本。

我目前沒有確切的解決方案,但可以繼續使用a similar thread

希望它有幫助!