刚看到一个关于图片合成的帖子,感觉写的很好,提供了一个思路,但没时间尝试,就先转载过来,有时间了试试。
在台湾的论坛里看到了一个关于图片合成的帖子,本想下下来研究,怎奈还要积分!所以仿照代码自己写了一个发出来和大家共同研究。
1.创建一个基于View-base的的工程命名为CombineImages
2.看看Xcode自动生成的文件,然后选择class右键创建新的file文件命名为CombineImages
3.打开CombineImages.h修改代码如下:
@interface CombineImagesViewController : UIViewController { IBOutlet UIImageView *ImageView1; IBOutlet UIImageView *ImageView2; IBOutlet UIImageView *newImageView; } @property (nonatomic, retain) UIImageView *ImageView1; @property (nonatomic, retain) UIImageView *ImageView2; @property (nonatomic, retain) UIImageView *newImageView; - (IBAction) Combine2Images:(id)sender; - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2;
4.打开CombineImages.m,修改并添加如下代码:
@synthesize ImageView1,ImageView2,newImageView; - (IBAction)Combine2Images:(id)sender{ newImageView.image = [self addImage:ImageView1.image toImage:ImageView2.image]; } - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2{ UIGraphicsBeginImageContext(image1.size); [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)]; [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)]; UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultingImage; }
- (void)viewDidLoad { [super viewDidLoad]; ImageView1.image = [UIImage imageNamed:@"ImageView1.png"]; ImageView2.image = [UIImage imageNamed:@"ImageView2.png"]; } - (void)dealloc { [ImageView1 release]; [ImageView2 release]; [newImageView release]; [super dealloc]; }
好了代码部分已经添加结束!
原文地址:http://www.devdiv.com/thread-45031-1-3.html