UIImage合并和裁切

本文介绍如何在iOS应用中实现图片截取、图片合成及保存图片到相册等功能,包括具体的代码实现过程。

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

[plain] view plain copy
  1. -(UIImage *)getImageFromImage{  
  2.   
  3. //大图bigImage  
  4.   
  5. //定义myImageRect,截图的区域  
  6.   
  7. CGRect myImageRect = CGRectMake(10.0, 10.0, 57.0, 57.0);  
  8.   
  9. UIImage* bigImage= [UIImage imageNamed:@"k00030.jpg"];  
  10.   
  11. CGImageRef imageRef = bigImage.CGImage;  
  12.   
  13. CGImageRef subImageRef = CGImageCreateWithImageInRect(imageRef, myImageRect);  
  14.   
  15.    
  16. CGSize size;  
  17.   
  18. size.width = 57.0;  
  19.   
  20. size.height = 57.0;  
  21.   
  22. UIGraphicsBeginImageContext(size);  
  23.   
  24. CGContextRef context = UIGraphicsGetCurrentContext();  
  25.   
  26. CGContextDrawImage(context, myImageRect, subImageRef);  
  27.   
  28. UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];  
  29.   
  30. UIGraphicsEndImageContext();  
  31.   
  32.    
  33. return smallImage;  
  34.   
  35. }  

Combine two UIImage

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. }  

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值