UIImagePNGRepresentation save a portrait image in landscape orientation - iphone

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.

Related

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.

UIPasteBoard Changes my Image Orientation

I am Copying the array of Images in UIPasteBoard and When I try to Paste the images in mail the Image orientation is changing. This is How I am Copying the Images.
UIPasteboard *pasteBoard = [UIPasteboard generalPasteboard];
pasteBoard.images = imageArray;
How can I copy the images without changing the Image orientation ?
UIImage's imageWithCGImage:scale:orientation: method can modify the orientation if you're not pleased with the results.
What your mail app is displaying is probably the true pixel array before the orientation info is applied to rotate it.
http://developer.apple.com/library/ios/documentation/UIKit/Reference/UIImage_Class/Reference/Reference.html#//apple_ref/occ/clm/UIImage/imageWithCGImage:scale:orientation:
I have this working. I just use the setData to give it raw data and then set the data type using forPasteboardType. Right below your
if (fileExists){
Try this
NSData *data = [NSData dataWithContentsOfFile:imagefile];
[pasteboard setData:data forPasteboardType:#"public.png"];
May this Helping to you.

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.

How to save a png image to camera roll?

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

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