ios合并两张图片(叠加两张图片 重合两张图片)

时间:2022-06-18 11:28:13

第一张图片
ios合并两张图片(叠加两张图片 重合两张图片)
第二张图片
ios合并两张图片(叠加两张图片 重合两张图片)
效果图
ios合并两张图片(叠加两张图片 重合两张图片)

创建一个  UIImageView  (黑斜体是关键一行代码)

UIImageView *imgView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 375, 667)];

imgView.image = [self addImage:@"psb33" withImage:@"littlesun"];


imgView.contentMode = UIViewContentModeScaleAspectFit;
[self.view addSubview:imgView];

方法实现:

- (UIImage *)addImage:(NSString *)imageName1 withImage:(NSString *)imageName2 {

UIImage *image1 = [UIImage imageNamed:imageName1];
UIImage *image2 = [UIImage imageNamed:imageName2];

UIGraphicsBeginImageContext(image1.size);

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

[image2 drawInRect:CGRectMake((image1.size.width - image2.size.width)/2,(image1.size.height - image2.size.height)/2, image2.size.width, image2.size.height)];

UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return resultingImage;
}