为了账号安全,请及时绑定邮箱和手机立即绑定

iOS-合并两个不同大小的图像

iOS-合并两个不同大小的图像

墨色风雨 2019-12-20 11:10:26
我面临以下问题:由于合并,我必须合并两个图像A和B以创建新图像C。我已经知道如何合并两个图像,但是在这种情况下,我的目标有所不同。我希望图像A将作为图像B的背景。例如,如果图像A的尺寸为500x500,图像B的尺寸为460x460,我希望图像C(合并的图像结果)为500x500,图像B( 460x460)居中。在此先感谢您的帮助或建议
查看完整描述

3 回答

?
慕的地10843

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


查看完整回答
反对 回复 2019-12-20
?
慕标5832272

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


查看完整回答
反对 回复 2019-12-20
?
慕桂英3389331

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;

}


查看完整回答
反对 回复 2019-12-20
  • 3 回答
  • 0 关注
  • 662 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信