Images rotated to 90 degree left email attachment iPhone - iphone

Well this may be a very easy Question but I'm not getting the answer after searching a lot.
I have one application in which I send the image taken from camera. The path of image captured from camera is stored in database. So in mail attachment code I load image from path and attach like this:
UIImage *myImage = [UIImage imageWithContentsOfFile:ImagePath];
NSData *imageData = UIImagePNGRepresentation(myImage);
[mailer addAttachmentData:imageData mimeType:#"image/png" fileName:#"Spotted"];
but the image is rotated by 90 Degree left every time. Can anyone guide me what am I doing wrong here??
P.S.: NSLog of ImagePath -->
/var/mobile/Applications/4BFB1BD9-DD83-42AF-A2BF-A5E4CC0DEAE3/Documents/459443.png

There has been some discussion on the Apple site about this issue in pictures sent by mail.

Unless you need it to be a PNG, I suggest converting it to a JPEG. JPEGs are more compressed (useful when it comes to email) and don't seem to have this rotating problem. Try using NSData *imageData = UIImageJPEGRepresentation(myImage); instead.
I've twisted my brain on this problem for several days as well. If you need it to be a PNG, here's a pretty good write up of what I learned on the topic: iOS PNG Image rotated 90 degrees

Related

How to convert all image format into .png format?

I am making a small application where I am receiving images from web-service. I am getting the image url. And the format of images is different different like .jpg,.gif,.png etc. And I want to save these images into my sandbox and later on show it into image view.
To do this I am giving a self name and gave the extension to .png. But at the time of retrieving the .png images are shown but different one's are not shown. So can you give me any idea how to convert all images to .png format or any other approach to do this?
after you retrieve your images/url save them this way:
NSData *imageData = UIImagePNGRepresentation(image);
[imageData writeToFile:savedImagePath atomically:YES];
Would it not be easier if you use an image library like SDWebImage which handles all the asynchronous image downloading and caching of the image onto your device?
https://github.com/rs/SDWebImage
Then you can do something like:
[myImageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];

Unable to convert a PNG image using UIImagePNGRepresentation

I'm writing a function to download a regular PNG file from web server and save it to iPhone Photos album. I don't get any error for my code listed below but UIImagePNGRepresentation doesn't perform the convert at all. All images I got were black. I have to enable "auto-enhanced" under Photos to display those image properly. Any clue what's wrong with my code? Thanks in advance.
UIImage *orgImg = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://server.com/test.png"]]];
NSData * convertedData = UIImagePNGRepresentation(orgImg);
UIImage * img = [UIImage imageWithData: convertedData];
// Save Image to Photo Album
UIImageWriteToSavedPhotosAlbum(img, self,
#selector(thisImage:hasBeenSavedInPhotoAlbumWithError:usingContextInfo:), nil);
Some more details of these two output images, orgImg and img:
If I pass orgImg to UIImageWriteToSavedPhotosAlbum directly, I did get the same PNG image hosted on the server. However Photos application on my iPhone displays it as a black image because Photos doesn't enhance PNG format automatically.
As for the img I got after using UIImagePNGRepresentation, the image data has been changed by the method, some meta data such as width/height have been removed from the original image. However Photos app on my iPhone still display it as an all black image.
Both images display properly if I copy them into either Safari or email. I didn't get any problem if I use JPEG in the server side either. Just curious why only PNG format doesn't work.

memory leak when JPEG, not when PNG

My application aims at saving some user photos in a PDF file in order to send the file by email. To produce a small size pdf, I want to compress my images in jpeg. When I draw jpeg to the PDF context, the pdf file is indeed much smaller than when I use PNG, but the use of JPEG leaks.
For my debugging I added a jpeg and a png file to my project.
The following call leaks :
UIImage * destImage = [UIImage imageNamed:#"Image.JPG"];
[destImage drawInRect:drawingFrame];
whereas this one does not :
UIImage * destImage = [UIImage imageNamed:#"Image.png"];
[destImage drawInRect:drawingFrame];
Is there something I'm missing ? Is it a know issue ?
I'm thinking about a workaround that would consist in using a PNG representations of my images and set a specific compression option to the pdf I generate, but did not find this in the pdf generation sdk.
Do you have an idea about it ?
Thanks in advance.

Problem uploading image on multi-part form via iPhone

I am trying to write an iPhone app that will allow me to upload a jpg via multi-part form.
Sometimes, the uploaded photo looks corrupted on the server with weird pixels and the photo looks cropped.
What could be the reason for that?
How come I don't get a http error response from the server? (upload backend is written in PHP)
You can use base64 encoding like shown below
UIGraphicsBeginImageContext(CGSizeMake(145.0, 145.0));
[takephoto.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *LargeImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = UIImageJPEGRepresentation(LargeImage, 0.25);
NSString *strImageFinalBASE64 =[data base64Encoding];
takePhoto can be your UIImageView:
IBOutlet UIImageView *takephoto;

UIImagePNGRepresentation slow or am I doing something wrong?

I'm working on an iPhone App that uses the camera to take pictures, then I'm saving them to the Applications Documents directory. I'm using the following code to convert the UIImage to NSData,
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
Then I write the NSData using
[imageData writeToFile:path atomically:NO]
It all works. The problem is that UIImagePNGRepresentation() is really slow. It takes 8-9 secs on my 3G to convert the image to NSData. This seems wrong to me. Does anyone have any experience with this? Is this just slow function or am I doing something terribly wrong?
Thanks
Are you sure you want to save pictures captured with the camera as PNG?
JPEG is a more appropriate format for photographs. Additionally, its likely much faster!