How to save a png image to camera roll? - iphone

I'd like to save screenshots from my game to camera roll.
Right now I'm using UIImageWriteToSavedPhotosAlbum which saves the image in jpg format with way too much ugly jpg artifacts.
Is there a way to save an UIImage in png format to camera roll?

First you have to add AssetsLibrary Framework, than like this:
ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
UIImage *image = ... some image
[library writeImageDataToSavedPhotosAlbum: UIImagePNGRepresentation(image) metadata:nil completionBlock:nil];

The solution pointed out here is now deprecated - this post:
UIImageWriteToSavedPhotosAlbum saves to wrong size and quality
shows how to do this:
UIImage* im = [UIImage imageWithCGImage:myCGRef]; // make image from CGRef
NSData* imdata = UIImagePNGRepresentation ( im ); // get PNG representation
UIImage* im2 = [UIImage imageWithData:imdata]; // wrap UIImage around PNG representation
UIImageWriteToSavedPhotosAlbum(im2, nil, nil, nil); // save to photo album

Napolit "I'd like to save screenshots from my game to camera roll. Right now I'm using UIImageWriteToSavedPhotosAlbum which saves the image in jpg format with way too much...."
Have just stumbled across your question, most of which I can't answer. however, I do have an EASY & QUICK WAY to grab iPad screens. Simply press the SLEEP/WAKE button and the HOME (round button at bottom of the screen) button at the same time.

This tells how to represent a UIImage as a jpg or png: Save UIImage Object as PNG or JPEG File

Related

UIImagePNGRepresentation save a portrait image in landscape orientation

After I get an image from iPhone gallery using imagePickerController I want to save that image in the app document directory, so I use this code:
-(void)saveImage:(UIImage*)image{
NSString *imagePath = [NSHomeDirectory() stringByAppendingPathComponent:#"Documents/background.png"];
[UIImagePNGRepresentation(image) writeToFile:imagePath atomically:YES];
}
but the resulting file is in Landscape orientation also if the image was in Portrait orientation...
If I try to see the image in a imageView on the xib the image result correct... (for example a 2448 × 3264 pic is showed right) but after I have saved that image the file that has been created is 3264 × 2448...
Anyone know what can I do to avoid this change?
Thanks a lot. Massy
I finally understood why this was happening and find a solution... solution is to save Jpeg and not PNG as PNG lose the orientation when saving.

How to set image in UIImageView from image of audio file in AVAudioPlayer?

I want to make basic Audio Player app by programatically.
If every Audio file contain image then I want that in UIImageView automatically set image from audio file.
If any file doesn't contain any image file then it should be set default image.
Like i shown below:
check how you are taking those images.
-case its Networking, use SDWebImage, take a look here:
https://github.com/rs/SDWebImage
-case those images are saved in a core data (example) make a search in your data, if exists,
pick that data and convert it to an image
UIImage * image = [UIImage imagewithData:dataImage];
remember you need to have a database image online or offline. Then just make a search, case exists, put the image, case not, default image.
The code that you need can be found in Apple's Example as referenced in the question that overbombing referenced. They make it easy for you:
MPMediaItem *currentItem = [musicPlayer nowPlayingItem];
// Assume that there is no artwork for the media item.
UIImage *artworkImage = noArtworkImage;
// Get the artwork from the current media item, if it has artwork.
MPMediaItemArtwork *artwork = [currentItem valueForProperty: MPMediaItemPropertyArtwork];
// Obtain a UIImage object from the MPMediaItemArtwork object
if (artwork) {
artworkImage = [artwork imageWithSize: CGSizeMake (30, 30)];
}
I would advise reading through Apple's Example anyway it seems you are doing pretty much exactly what they did.

UIImage display differently when saving and loading from file

I'm download jpeg picture from web server and load it to UIImage.
If I display the UIImage in UIImageView directly, I see the picture correctly.
But if i cache the image to file with :
[UIImageJPEGRepresentation(image,1.0f) writeToFile:sFilePath atoimcally:YES]
and load it with :
image=[UIImage imageWithContentsOFile:sFilePath]
and display this in the same UIImageView, I can see white stripes in the sides of the picture.
again, Exactly the same UIImageView object with the same properties settings in it.
Why is that?
You can simply write to a file the NSData you have loaded from the web, without going through the UIImageJPEGRepresentation routine.
[dataObject writeToURL:fileURL atomically:YES];
and to retrieve
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[fileURL path]];
That works really well in my app.

Merging photos - iPhone SDK

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!
If you've displayed the frame over the photo in your UI, just use UIScreenGetImage something like
...
CGImageRef screen = UIGetScreenImage();
UIImage* image = [UIImage imageWithCGImage:screen];
CGImageRelease(screen);
// You could, e.g., save the captured image to photo album
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image:didFinishSavingWithError:contextInfo:), nil);
This probably isn't what you want, but if you load both images into OpenGL (there's a nice Apple sample that loads images in OpenGL), lay one on top of the other, and then write the result to an image (excellent tutorial here - http://www.bit-101.com/blog/?p=1861).
You don't even need to render it to the screen, so there's no fiddling around with EAGL.

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];