How to overlay one image over another image in iPhone - iphone

In my app i have one transparent image, now when user selects one image from photo library, that image have to be display over transparent image and make as an one uiiimage so that user can mail or share with it. I have used the following code, however image is not coming correct over transparent image
UIImage *backgroundImage = [UIImage imageNamed:#"iPhoneOverLay.png"];
UIGraphicsBeginImageContext(backgroundImage.size);
[backgroundImage drawInRect:CGRectMake(0, 0, backgroundImage.size.width, backgroundImage.size.height)];
[testImage drawInRect:CGRectMake(backgroundImage.size.width - testImage.size.width, backgroundImage.size.height - testImage.size.height, testImage.size.width, testImage.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Here testImage is selected from Photo Library or taken from camera

You draw the 2 images in the wrong order. Transparency is a property that makes things shine through that have been painted previously. Therefore you have to draw the (opaque) photo first and the (transparent) overlay second.

Related

ios dropbox, loading thumbnail of image in table view

I have picture in my dropbox, which i need to show in Uitable view. I am able to show the picture by name, using metadata filemetadata, and when i click on any of cell i am successfully able to view the image in image view, by using the call to loadthumbnail.
But now, I want to show the thumbnail of image together with, image name, so that user have idea of image before opening it.
So, what is the simple way of doing it.
You should resize (thumb) the original UIImage's prior to showing the UITableView and save them in your app cache folder.
Something like this (out the top of my head)
CGSize targetSize = (CGSize){ 100, 80 };
UIGraphicsBeginImageContext(targetSize);
CGRect thumbnailRect = (CGRect){ 0, 0, targetSize.width, targetSize.height };
[sourceImage drawInRect:thumbnailRect];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// After this you should store the UIImage (NSData) as a file, so you could use it later on.
Try to go to the Start Menu then look for My Documents and look for a folder named Downloads. This worked for me on a download i could not see.
Good Luck!

uitableviewcell resize image

i load data in a tableview, each row have a simple picture 500x300px that go resize to size of cell.
Is possible resize the images?
tableview.m
NSString *filePath = [[NSBundle mainBundle] pathForResource:receip.image ofType:#"jpg"];
UIImage *image = [UIImage imageWithContentsOfFile:filePath];
cell.imageView.image = image;
I assume you mean resize them before putting them in the cell to improve performance?
You can do this with core graphics as follows:
//define desired size
CGSize size = CGSizeMake(100.0f, 100.0f);
//create drawing context
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0f);
//draw image at new size
[image drawInRect:CGRectMake(0.0f, 0.0f, size.width, size.height)];
//capture resultant image
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Be warned though that is a computationally expensive task. You'll probably want to resize all your images in advance, possibly on a background thread, and store the resized versions in an array. You definitely do not want to stick the code above in your cellForRowAtIndexPath method, or your table scrolling performance will be diabolical.
If those images come from an external source (say the web). It's better to first cache the original file and then (like explained by #Nick Lockwood) resize them (remember retina displays need twice the width / height for hi-res displaying). And after that process is complete you can send a signal that the "download" is complete and carry on showing the UITableView
If you already have the images in your app bundle I suggest you make a thumb version of that and add it to your bundle. For better performance o'course.

Adding picture frame to a photo

I am making an app that adds a picture frame to a photo.I would like to know how to have my Save button save both Images (the photo, and the frame) as one Image.Right now it only saves one of the images.
In interface builder I have the save action saving the image that is loaded into an ImageView, with the frame ImageView overlaying that image.
I'd like to merge the two photos as one, so the save action can save the image with the frame.
Thanks!
In this may need to use the masking in the iphone where the unnecessary thing of the image is automatically remove and attach with the frame.
I think this help to implement best for the your applications
So you can refer the following link for Download and tutorial and Source also.
Reference link
You need to do some drawing using Core Graphics. This code should do what you want, possibly with some tweaks to the rectangles/sizes:
UIGraphicsBeginImageContext(image.size);
[image drawRect:CGRectMake(0, 0, image.size.width, image.size.height);
[frameImage drawRect:CGRectMake(0, 0, image.size.width, image.size.height);
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();

Custom image mask in iOS

I have a issue with masking images. I do game "puzzle" and have to make custom images. I found and tried 2 way of custom cropping:
Using CALayer.mask property.
Using UIImage.mask property.
In first option i create my custom path, then assign it to CAShapeLayer.path property, then assign CAShapeLayer to CALayer.mask property. At the end i have custom cropped image.
In second option i use firstly use CGImageMaskCreate() method (i use previously created black mask images of puzzle), then CGContextClipToMask().
In either options i have problem with performance (mostly when i crop image into 16 puzzles and drag in over the screen).
Is there any other approaches to crop image in custom way.
(I don't know how to solve performance problem).
Thanks in advance.
There are lots of UIImage-categories out there you can use for this. Give me a moment and I'll post some links here:
Cropping an UIImage (not really a category though, but it'll fit)
UIImage: Resize, then Crop
https://sites.google.com/a/injoit.com/knowledge-base/for-developers/graphics/uiimage-routines-scaling-cropping-rotating-etc
http://www.hive05.com/2008/11/crop-an-image-using-the-iphone-sdk/
http://maybelost.com/2010/11/cropping-a-uiimage-on-iphone/
Try this:
-(UIImage *)imageByCropping:(UIImage *)imageToCrop toRect:(CGRect)rect
{
CGImageRef imageRef = CGImageCreateWithImageInRect([imageToCrop CGImage], rect);
UIImage *cropped = [UIImage imageWithCGImage:imageRef];
CGImageRelease(imageRef);
return cropped;
}
...
UIImage *temp_image = [self imageByCropping:original_image toRect:clipping_rectangle];
Maybe you should consider about drawing the image in a new image with an alpha background an overdrawing the current background. I mean: All pixel which are inside the jigsaw piece: normal colour, all pixels outside the jigsaw piece = transparent. And then try to blend it to the new background or overdrawing it.
Just my 2 cents. :)

How to save a stretched image?

How can I save a stretched image?
I have a small(50 X 50) image, that I have stretched while displaying it on a UIImageView (by choosing scaletofit mode.) Now I need to fetch & save that stretched image.
I have tried by using UIImageView's image property, but it gives me original image not that stretched image.
So, do you have any idea how to solve this problem? If then please help me or guide me or at-least give me a hint.
Hoping for your reply.........
Thanks
You have to create an image by copying the bit map of the view's CGContext (core graphic context). This has been asked on stackoverflow before but I can't find the answer right now.
You need to render your UIImageView into a graphic context you create :
UIGraphicsBeginImageContext(yourUIImageView.frame.size);
yourUIImageView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
//get the PNG representation of the UIImage (but you could do the same with JPEG
NSData *myData = UIImagePNGRepresentation(screenshot);
//then save it to a file
[myData writeToFile:thePathToWriteYourFileAsNSString atomically:YES];