Iphone Sdk重叠2张图片合二为一

时间:2010-09-03 17:44:05

标签: iphone uiimage

我有2张图片..我怎么能重叠它们才能获得1张UIImage?将图像2的位置设置在图像1的X,Y

谢谢!

2 个答案:

答案 0 :(得分:4)

Zapacila,你找到了问题的答案吗?你可以这样做:

#define imageWidth 40
#define imageHeight 60

UIImage *image1 = [UIImage imageNamed:@"firstimage.png"];
UIImage *image2 = [UIImage imageNamed: @"secondimage.png"];

CGSize itemSize = CGSizeMake(imageWidth, imageHeight);

UIGraphicsBeginImageContext(itemSize);

CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
[image1 drawInRect:imageRect];
[image2 drawInRect:imageRect];

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

UIImage overlappedImage是一个新图像,包含重叠的初始图像。老实说,我不知道这是否是实现这一结果的最佳方法,但我知道这绝对有效。

如果在此期间您找到了更有效的解决方案,请告诉我们!

答案 1 :(得分:0)

为什么你不只是有一个UIView并将这两个图像作为子视图放入?

UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimage.png"]];
UIView *imagesView = [[UIView alloc] initWithFrame:image.frame];
[imagesView addSubview:image];

UIImageView *imageToOverlay = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"myimagetooverlay.png"]];
[imageToOverlay setCenter:CGPointMake(10,10)];
[imagesView addSubview:imageToOverlay];

[self.view addSubview:imagesView];