在另一个UIImage上添加UIImage

时间:2012-05-09 19:50:40

标签: ios objective-c uiimage

我有2张图片:第一张是用户个人图片,第二张是图标(徽章)。

我想在第一个uiimage(用户图像)的左下角添加第二个uiimage(图标)并将它们保存到新的Uiimage中。

感谢

2 个答案:

答案 0 :(得分:28)

试试这个方法:

-(UIImage *)drawImage:(UIImage*)profileImage withBadge:(UIImage *)badge
{
    UIGraphicsBeginImageContextWithOptions(profileImage.size, NO, 0.0f);
    [profileImage drawInRect:CGRectMake(0, 0, profileImage.size.width, profileImage.size.height)];
    [badge drawInRect:CGRectMake(0, profileImage.size.height - badge.size.height, badge.size.width, badge.size.height)];
     UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
     UIGraphicsEndImageContext();
     return resultImage;
}

您可以通过以下方式使用它:

UIImage *myBadgedImage = [self drawImage:profileImage withBadge:badgeImage];

答案 1 :(得分:2)

试试这个:

CGFloat scale = [self currentScale];
if (scale > 1.5)
    UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, scale);
else
    UIGraphicsBeginImageContext(view.frame.size);

[image1 drawInRect:CGRectMake(0, 0, w1, h1)];
[image2 drawInRect:CGRectMake(0, 0, w2, h2)];

UIImage* screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();