Blending UIImage with alpha value - iphone

How to blend two images by changing alfa of only one image so that the upper image will be slightly transparent and the above image will be displayed as it is drawn on the background image.

You can do it through imageView alpha property
imageView.alpha = 0.0f;
Detailed code:
UIImage *bottomImage = [UIImage imageNamed:#"bottomImage.png"];
UIImage *topImage = [UIImage imageNamed:#"topImage.png"];
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[topImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
see more details here

Related

How to position an object over an image and store it

I am creating a "cartoonizer" application, which takes an image as an input and modifies it so as to format it in comics style.
At the moment, the original image is stored in a UIImageView, and I access to it by doing:
imageView.image
I was wondering whether it is possible to position an object on the image imageView.image (like a balloon) and then store the image with the object on it, like if it was originally part of the image content.
Thank you in advance.
You can do it like this, assuming you have correct opacity in the top image.
Like you said you get your image from imageView.image:
From here: blend two uiimages based on alpha/transparency of top image
UIImage *bottomImage = [UIImage imageNamed:#"bottom.png"];
UIImage *image = [UIImage imageNamed:#"top.png"];
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
More compositing techniques here: http://mobile.tutsplus.com/tutorials/iphone/ios-sdk-advanced-uiimage-techniques/

Not able to merge two images into one

I'm trying to merge two images in one, and save that image onto the camera roll. But it just show a blank image. Can anyone help?
My code:
-(void)SaveFinalImage{
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *savedImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(savedImg, nil, nil, nil);
}
I have used this in my app.
UIImage *bottomImage = [UIImage imageNamed:#"bottom.png"]; //background image
UIImage *image = [UIImage imageNamed:#"top.png"]; //foreground image
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
for more see my related answer on the same subject. iOS - Merging two images of different size

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.
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();
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
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];

How to clip image with another image in CGContext?

I want to get the following result with two images.
Please help me.
To combine two images on an image view try this
UIImage *bottomImage = [UIImage imageNamed:#"bottom.png"]; //background image
UIImage *image = [UIImage imageNamed:#"top.png"]; //foreground image
CGSize newSize = CGSizeMake(width, height);
UIGraphicsBeginImageContext( newSize );
// Use existing opacity as is
[bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
// Apply supplied opacity if applicable
[image drawInRect:CGRectMake(0,0,newSize.width,newSize.height) blendMode:kCGBlendModeNormal alpha:0.8];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Add newImage to UIImageView
After #Sumanth's code for combine two images you need to mask final image like how-to-mask-an-image link

Setting a UIImage border

Is there a way to set borders to a UIImage. I know how to set it for UIImageView, but my problem is the UIImage that I load up in a UIImageview wont be of the same size or aspect ratio as the UIImageView. Hence Ive kept the UIImageView mode to aspect fit. Giving a border to UIImageView now would border the entire UIImageView rather than just the UIImage, and that doesn't look good when the UIImage is not of the same size or aspect ratio as the UIV.
Help?
There's some solutions in this question: How can i take an UIImage and give it a black border?
#import <QuartzCore/CALayer.h>
UIImageView *imageView = [UIImageView alloc]init];
imageView.layer.masksToBounds = YES;
imageView.layer.borderColor = [UIColor blackColor].CGColor;
imageView.layer.borderWidth = 1;
imageView.layer.cornerRadius = 10; //optional
image with border
+(UIImage*)imageWithBorderFromImage:(UIImage*)source
{
CGSize size = [source size];
UIGraphicsBeginImageContext(size);
CGRect rect = CGRectMake(0, 0, size.width, size.height);
[source drawInRect:rect blendMode:kCGBlendModeNormal alpha:1.0];
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetRGBStrokeColor(context,(255/255.f),(255/255.f),(255/255.f), 1.0);
CGContextStrokeRect(context, rect);
UIImage *testImg = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return testImg;
}