如何在iOS中的另一个图像的中心合并一个图像?

时间:2015-10-28 13:18:19

标签: ios objective-c uiimageview

我有一个图像是视频的缩略图。我想在该缩略图图像的中心添加播放图像,以便我可以识别它是一个视频。请告诉我该怎么做。我使用下面的代码来合并图像,但这对我不起作用。

mux

2 个答案:

答案 0 :(得分:4)

如果您不想使用两个ImageView

- (UIImage *) getMergeImage:(UIImage *)parentImage childImage:(UIImage *)childImage{

CGSize size = CGSizeMake(parentImage.size.width, parentImage.size.height);

UIGraphicsBeginImageContext(size);

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

UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return finalImage;
}

答案 1 :(得分:0)

我修复了 image1 和 image2 并添加了 autoreleasepool 以避免内存问题:

- (UIImage *)getMergeImage:(UIImage *)parentImage childImage:(UIImage *)childImage{
@autoreleasepool {
    CGSize size = CGSizeMake(parentImage.size.width, parentImage.size.height);
    
    UIGraphicsBeginImageContext(size);
    
    [parentImage drawInRect:CGRectMake(0,0,size.width, size.height)];
    [childImage drawInRect:CGRectMake( ((size.width - childImage.size.width) / 2 ),((size.height - childImage.size.height) / 2 ),childImage.size.width, childImage.size.height)];
    
    UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return finalImage;
}

}