UIImage的合并和裁剪

参考:http://iphoneincubator.com/blog/tag/uiimage

根据给定得图片,从其指定区域截取一张新得图片

-(UIImage *)getImageFromImage{

//大图bigImage

//定义myImageRect,截图的区域

CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);

UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];

CGImageRef imageRef = bigImage.CGImage;

CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);

 
CGSize size;

size.width = 57.0;

size.height = 57.0;

UIGraphicsBeginImageContext(size);

CGContextRef context = UIGraphicsGetCurrentContext();

CGContextDrawImage(context, myImageRect, subImageRef);

UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];

UIGraphicsEndImageContext();

 
return smallImage;

}

Combine two UIImages

To add two UIImages together you need to make use of Graphics Context.

  1. - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {  
  2.     UIGraphicsBeginImageContext(image1.size);  
  3.   
  4.     // Draw image1  
  5.     [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];  
  6.   
  7.     // Draw image2  
  8.     [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];  
  9.   
  10.     UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  
  11.   
  12.     UIGraphicsEndImageContext();  
  13.   
  14.     return resultingImage;  
  15. }  

Create a UIImage from a part of another UIImage

This requires a round-trip to Core Graphics land:

  1. - (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {  
  2.     CGImageRef sourceImageRef = [image CGImage];  
  3.     CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);  
  4.     UIImage *newImage = [UIImage imageWithCGImage:newImageRef];  
  5.     CGImageRelease(newImageRef);  
  6.     return newImage;  
  7. }  

Save UIImage to Photo Album

This is just a one-liner:

  1. UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), context);  

And to know if the save was successful:

  1. - (void)imageSavedToPhotosAlbum:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {  
  2.     NSString *message;  
  3.     NSString *title;  
  4.     if (!error) {  
  5.         title = NSLocalizedString(@"SaveSuccessTitle", @"");  
  6.         message = NSLocalizedString(@"SaveSuccessMessage", @"");  
  7.     } else {  
  8.         title = NSLocalizedString(@"SaveFailedTitle", @"");  
  9.         message = [error description];  
  10.     }  
  11.     UIAlertView *alert = [[UIAlertView alloc] initWithTitle:title  
  12.                                                     message:message  
  13.                                                    delegate:nil  
  14.                                           cancelButtonTitle:NSLocalizedString(@"ButtonOK", @"")  
  15.                                           otherButtonTitles:nil];  
  16.     [alert show];  
  17.     [alert release];  
  18. }  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值