Pages

Saturday, September 10, 2011

Combine Images In IOS


Combine two images:
UIImage *firstImage = [UIImage imageNamed:@"a.jpg"];

UIImage *secondImage = [UIImage imageNamed:@"b.jpg"];

CGSize size = CGSizeMake(firstImage.size.width+secondImage.size.width,firstImage.size.height);
UIGraphicsBeginImageContext(size);
[firstImage drawAtPoint:CGPointMake(0,0)];

[secondImage drawAtPoint:CGPointMake(firstImage.size.width,0)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
[self saveImage:newImage withName:@"ABC.png"];

UIGraphicsEndImageContext();

Store image in document.


-(void)saveImage:(UIImage *)image withName:(NSString *)name
{
NSData *data = UIImagePNGRepresentation(image);
// NSData *data = UIImageJPEGRepresentation(image, 1);
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString* documentsDirectory=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES)
objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:name];
[fileManager createFileAtPath:fullPath contents:data attributes:nil];
}

RESULT:



Thanks.............


No comments:

Post a Comment