Surjeet Singh
Surjeet Singh

Reputation: 11939

Merging two image from different imageView in a single image in iPhone

i want to add two images from diffent imageview to give them 3d look such that both images overlap one another and when user show the image its shows both images and when user move their iphone by sides it show one image (only one image displayed and anothers be disappear) like a hologram.

Please help me someone its very important for me. thank you......!!

Upvotes: 3

Views: 1434

Answers (3)

Shardul
Shardul

Reputation: 4274

You can use this code.

+ (UIImage*)mergeImage:(UIImage*)first withImage:(UIImage*)second
{
 // get size of the first image
 CGImageRef firstImageRef = first.CGImage;
 CGFloat firstWidth = CGImageGetWidth(firstImageRef);
 CGFloat firstHeight = CGImageGetHeight(firstImageRef);

 // get size of the second image
 CGImageRef secondImageRef = second.CGImage;
 CGFloat secondWidth = CGImageGetWidth(secondImageRef);
 CGFloat secondHeight = CGImageGetHeight(secondImageRef);

 // build merged size
 CGSize mergedSize = CGSizeMake(MAX(firstWidth, secondWidth), MAX(firstHeight, secondHeight));

 // capture image context ref
 UIGraphicsBeginImageContext(mergedSize);

 //Draw images onto the context
 [first drawInRect:CGRectMake(0, 0, firstWidth, firstHeight)];
 [second drawInRect:CGRectMake(0, 0, secondWidth, secondHeight)]; 

 // assign context to new UIImage
 UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

 // end context
 UIGraphicsEndImageContext();

 return newImage;
}

Or try this link.

Upvotes: 1

Amit
Amit

Reputation: 1043

As you want to show the images like hologram and then show only one, a possible way would be:

  • Add both image to same rect initially adding as subViews and set transparency(alpha) to both images to be 0.5.
  • as user move the phone show the desired image by setting its alpha to 1.0 and bringing that subview to front.

It may help.

Upvotes: 3

Sumit Mundra
Sumit Mundra

Reputation: 3901

try this code for merge two image

UIGraphicsBeginImageContext(firstImage.size);
[firstImage drawAtPoint:CGPointMake(0,0)];
[secondImage drawAtPoint:CGPointMake(0,0)];

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

Upvotes: 2

Related Questions