UIImage的一些使用技巧

Image Processing Tricks

Recently there have been some interesting developer news related to working with images on the iPhone.

  • First there is Chris Greening’s open source project simple-iphone-image-processing, that provides a set of common image processing tasks .
  • Today I listened to the Mobile Orchard’s podcast Interview with Paul Cantrell, and the discussion was about UIKit, views, layers, etc. This was the most enlightening information I’ve come across on this topic ever. Highly recommended.

So, I thought I’d contribute a few UIImage routines that I’ve found useful.

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



转自: http://iphoneincubator.com/blog/tag/uiimage

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值