The Rock
The Rock

Reputation: 147

how to merge 2 images without using set alpha?

I am a Fresher Developer in iPhone .

I want Merge Two Images and Get Only One Image In UIImageView without set alpha.

This is my code. This code is working using alpha, but I want set without set alpha.

MYCODE:-

-(UIImage *)maskingImage:(UIImage *)image
{

    CGSize sizeR = CGSizeMake(200, 220);
   // UIImage *textureImage = [UIImage imageNamed:@"tt.png"];
    UIImage *textureImage =imgView2.image;



    UIGraphicsBeginImageContextWithOptions(sizeR, YES, textureImage.scale);
    [textureImage drawInRect:CGRectMake(0.0, 0.0, 200, 220)];

    UIImage *bottomImage = UIGraphicsGetImageFromCurrentImageContext();

    UIImage *upperImage = image;


    CGSize newSize = sizeR ;
    UIGraphicsBeginImageContext(newSize);
    [bottomImage drawInRect:CGRectMake(0.0, 0.0, 200, 220)];
    [upperImage drawInRect:CGRectMake(0.0, 0.0, 200, 220) blendMode:kCGBlendModeNormal alpha:0.5];



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

Thanks in advance.

Upvotes: 10

Views: 513

Answers (3)

SAHIL
SAHIL

Reputation: 431

i just faced the same problem , now i got the solution for my problem

 CGSize newSize = CGSizeMake(320, 377);
    UIGraphicsBeginImageContext( newSize );

    // Use existing opacity as is
    [ image1 drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    // Apply supplied opacity
    [image2 drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];

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

Try this ,its work like a charm for me , i hope you will also get the solution.

you can use like --

[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.0];

Upvotes: 1

Waseem Shah
Waseem Shah

Reputation: 2219

use this function

- (UIImage * ) mergeImage: (UIImage *) imageA
            withImage:  (UIImage *) imageB
             strength: (float) strength  X:(float )x Y:(float)y{

UIGraphicsBeginImageContextWithOptions(CGSizeMake([imageA size].width,[imageA size].height), NO, 0.0);

[imageA drawAtPoint: CGPointMake(0,0)];

[imageB drawAtPoint: CGPointMake(x,y)
          blendMode: kCGBlendModeNormal // you can play with this
              alpha: strength]; // 0 - 1

UIImage *mergedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return mergedImage;}

here x and y is the placemnt where you want to show second image

Upvotes: 1

SachinVsSachin
SachinVsSachin

Reputation: 6427

UIGraphicsBeginImageContext(YOUR SIZE);

//FIRST IMAGE
[FIRST_IMAGE drawInRect:CGRectMake(0, 0, YOUR_SIZE_WIDTH/2, YOUR_SIZE_HEIGHT)];
//SECOND IMAGE
[SECOND_IMAGE drawInRect:CGRectMake(YOUR_SIZE_WIDTH/2, 0, YOUR_SIZE_WIDTH/2, YOUR_SIZE_HEIGHT)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

Upvotes: 1

Related Questions