开发者

Iphone Sdk Overlap 2 images into one

开发者 https://www.devze.com 2023-01-14 23:21 出处:网络
I have 2 i开发者_开发技巧mages.. How can i overlap them so i can get 1 UIImage? Setting the position of image 2 inside image 1 at X, Y

I have 2 i开发者_开发技巧mages.. How can i overlap them so i can get 1 UIImage? Setting the position of image 2 inside image 1 at X, Y

Thanks!


Zapacila, have you found an answer to your question? You could do it like this:

#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();

The UIImage overlappedImage is a new image which contains the initial ones, overlapped. To be honest, I don't know if this is the best method to achieve this result, but I know it definitely works.

If in the meantime you found a more efficient solution, let me know!


Why don't you just have a UIView and put the two images into that as subviews?

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

精彩评论

暂无评论...
验证码 换一张
取 消