3 回答
TA贡献1785条经验 获得超8个赞
这是我在应用程序中所做的,但是没有使用UIImageView:
UIImage *bottomImage = [UIImage imageNamed:@"bottom.png"]; //background image
UIImage *image = [UIImage imageNamed:@"top.png"]; //foreground image
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
如果图像已经具有不透明度,则无需进行设置(如在bottomImage中),否则可以进行设置(与图像一样)。
在这之后UIImage创建,那么你可以将它嵌入到你的UIImageView
更新:感谢Ahmet AkkoK-对于Swift(2.2)用户,混合模式宏已更改。CGBlendMode .kCGBlendModeNormal被替换为CGBlendMode.Normal
TA贡献1966条经验 获得超4个赞
嘿,我得到了多张图像,并添加了具有不同前景的相同背景。
UIImage *bottomImage = [UIImage imageNamed:@"photo 2.JPG"]; //background image
UIImage *image = [UIImage imageNamed:@"photo 3.JPG"]; //foreground image
UIImage *image1 = [UIImage imageNamed:@"photo 4.JPG"]; //foreground image
UIImage *image2 = [UIImage imageNamed:@"photo 5.JPG"]; //foreground image
CGSize newSize = CGSizeMake(320, 480);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.4];
[image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.3];
[image2 drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.2];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
resultView = [[UIImageView alloc] initWithImage:newImage];
resultView.frame = CGRectMake(0, 0,320,460);
[self.view addSubview:resultView];
TA贡献2036条经验 获得超8个赞
刚刚为想要使用Srikar Appal答案的用户提供了快速粘贴功能。(如果背景图片和前景图片的大小不同)
- (UIImage *) mergeImages:(NSString *)bgImageFileName foreGround:(NSString *)fgImageFileName {
UIImage *bottomImage = [UIImage imageNamed:bgImageFileName]; //background image
UIImage *image = [UIImage imageNamed:fgImageFileName]; //foreground image
CGSize newSize = CGSizeMake(bottomImage.size.width, bottomImage.size.height);
UIGraphicsBeginImageContext(newSize);
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
// Change xPos, yPos if applicable
[image drawInRect:CGRectMake(11,11,image.size.width,image.size.height) blendMode:kCGBlendModeNormal alpha:1.0];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}
- 3 回答
- 0 关注
- 662 浏览
添加回答
举报