`
beike
  • 浏览: 360053 次
社区版块
存档分类
最新评论

iphone UIimage 常用 方法

阅读更多

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

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. }  
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
	UIGraphicsBeginImageContext(image1.size);

	// Draw image1
	[image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];

	// Draw image2
	[image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];

	UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

	UIGraphicsEndImageContext();

	return resultingImage;
}

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.     return  newImage;  
  6. }  
- (UIImage *)imageFromImage:(UIImage *)image inRect:(CGRect)rect {
	CGImageRef sourceImageRef = [image CGImage];
	CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
	UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
	return newImage;
}

Save UIImage to Photo Album

This is just a one-liner:

  1. UIImageWriteToSavedPhotosAlbum(image, self, @selector(imageSavedToPhotosAlbum: didFinishSavingWithError: contextInfo:), context);  
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];  

分享到:
评论

相关推荐

    iPhone常用代码集合

    ### iPhone常用代码集合详解 #### 图片处理代码 在iOS开发中,经常需要对图片进行裁剪或处理。以下代码展示了如何使用`UIImage`和Core Graphics框架中的`CGImageCreateWithImageInRect`函数来获取一张图片的部分...

    iphone开发常用代码

    ### iPhone开发常用代码知识点 #### 一、URL编码方法 在进行网络请求时,经常会遇到需要对字符串进行URL编码的情况,以确保传输过程中不会出现问题。下面的代码展示了一个简单的URL编码函数实现: ```objective-c...

    Iphone_开发常用代码

    ### iPhone开发常用代码知识点 #### 一、更改Cell选中背景 在iOS开发过程中,我们经常需要自定义UITableViewCell(单元格)的样式,包括改变选中状态时的背景颜色或图像。下面是一段示例代码: ```objective-c ...

    iOSUI基础控件常用方法探微

    ### iOS UI基础控件常用方法探微 #### 一、UIImageView 控件详解 **1.1 UIImageView 显示问题** 在 iOS 开发过程中,经常会遇到 UIImageView 的显示问题。默认情况下,若未设置 UIImageView 的尺寸,该控件将自动...

    IPhone3 开发秘籍 例程

    本章涵盖了加载、显示和处理图像的方法,如使用UIImage、UIImageView,以及Core Graphics和Core Image框架的应用。 **第八章:手势识别** 手势识别让应用能够理解和响应用户的触摸动作。本章介绍了...

    一些常用的工具类

    在iOS和iPhone开发中,工具类(Tool Classes)通常是开发者为了简化代码、提高效率而创建的一系列类别(Category)。这些类别扩展了系统原有的类,增加了新的方法或优化了已有的功能。"一些常用的工具类"这个主题,...

    模仿IPHONE滑动开关UISwitch

    在iOS中,我们可以使用`UIImage`的`imageWithContentsOfFile:`方法加载图片,然后通过`UISwitch`的`setOnImage:`和`setOffImage:`方法设置开和关状态的图片。确保这两张图片的尺寸相同,以便在切换时保持控件的大小...

    iphone 开发基础控件UISlider

    在iPhone应用开发中,UISlider常被用于音量调节、进度条控制等场景。本教程将详细介绍UISlider的常用属性设置、点击事件处理以及自定义滑块图片,非常适合iOS开发初学者学习。** ### 1. UISlider的基本使用 首先,...

    iphone uitableview图片延迟加载实例(详细注释)

    可以使用UIImage的category方法,如`imageWithCGImage:size:`来调整图片尺寸,以减少内存占用。 8. **代码注释**:实例中提到“加入了详细的注释”,这有助于开发者理解代码逻辑,特别是对初学者,清晰的注释能加速...

    iphone开发一年的工作笔记

    ### iPhone开发一年的工作笔记知识点详解 #### 文档概述 该文档是作者从事iOS开发一年以来,针对工作中常用到的200多个iOS知识点进行总结整理的成果。文档中不仅包含了知识点的简要介绍,还提供了可运行的示例代码...

    iOS7常用图标

    30x30像素的图标适用于iPhone 4S及更早的设备,对于iPhone 5/5S/SE(@2x)和iPhone 6/7/8/SE(2020)等设备,需要60x60像素的图标,而iPhone 6 Plus/7 Plus/8 Plus/X系列及以后的设备(@3x)则需要90x90像素的图标。...

    如何学习iPhone之开发过程中的一些小技术的总结--千锋培训

    常用的随机数生成方法有两种: 1. **使用`srandom()`与`random()`函数**: - 首先需要引入必要的头文件: ```objective-c #import #import ``` - 设置随机数种子,确保每次程序运行时生成不同的随机数序列:...

    (0161)-iOS/iPhone/iPAD/iPod源代码-图像(Image)-Image Cropper

    1. 图像对象与类:在iOS中,最常用的图像类是UIImage和CGImage。UIImage通常用于UI界面显示,而CGImage则代表图像数据的核心结构,可以被用来创建或修改图像。在处理图像时,这两个类会紧密配合。 2. 图像绘制:...

    ios 开发常用代码

    标题与描述中的“iOS开发常用代码”涉及到的是iOS应用程序开发中的常见编程实践和技术要点,主要聚焦于使用Objective-C或Swift语言进行UIKit框架下的界面元素定制和优化。以下将详细解析和扩展这部分内容所涵盖的...

    UIToolBar快速切换图标

    在iOS应用开发中,`UIToolbar` 是一个常用组件,它允许开发者在屏幕底部创建一个包含多个按钮或工具的栏。本主题聚焦于如何在`UIToolbar`上实现图标快速切换,这对于构建功能丰富的界面,提升用户体验具有重要意义。...

    IOS常用文档

    iPhone键盘颜色更改 #### 实现方式 仅当使用特定类型的数字键盘时(如`UIKeyboardTypeNumberPad`或`UIKeyboardTypePhonePad`),才能更改键盘的颜色。可以通过遍历应用程序的所有窗口和视图,找到键盘对象并修改其...

    Mac XCode iPhone 显示一个UIImageView 和一个UIWebView

    在iOS开发中,Xcode是Apple官方推荐的集成开发环境(IDE),用于创建iPhone、iPad以及Mac应用。本文将深入探讨如何使用Xcode显示一个UIImageView和一个UIWebView,这两个是iOS开发中常用的UI组件。 UIImageView是...

    iphone 仿qq聊天界面,实现文字表情的混排

    同时,对于表情图片,可以使用`UIImage`的`imageWithContentsOfURL:`异步加载,避免阻塞主线程。 9. **自定义表情键盘**:为了让用户方便输入表情,还需要实现一个自定义的键盘,通常可以使用`...

Global site tag (gtag.js) - Google Analytics