memory leak when JPEG, not when PNG - iphone

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.

Related

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.

Images rotated to 90 degree left email attachment 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

ALAssetRepresentation as NSData for GIFs

I'm try to allow users to pull images out of their Photos collections using ALAssetsLibrary. Users can then upload these images. My goal is to allow users to upload any GIFs they may have in their library w/o loosing any animation they may have.
For PNG and JPEG files I can grab the ALAssetRepresentation, use - (CGImageRef)fullResolutionImage to get a CGImageRef, and then save it to NSData using UIImageJPEGRepresentation or UIImagePNGRepresentation.
However, because no similar function exists for GIF files, all I can do is covert the GIF to either JPEG or PNG, but then I lose the animation.
Is there either
a way to grab the NSData straight from an ALAssetRepresentation object or
a way to go from ALAssetRepresentation -> CGImageRef -> NSData without loosing any gif animation frames?
Thanks in advance!
Yes, there is a quite simple way:
Use the getBytes:fromOffset:length:error: method of ALAssetRepresentation. This gives youthe raw file data of the ALAsset, in your case the GIF file.

No thumbnail in iPhoto for images saved with UIImageWriteToSavedPhotosAlbum

My application downloads JPEG images from the web and save them to the device using UIImageWriteToSavedPhotosAlbum. All works fine except for one issue: when I browse iPhone's photo library with iPhoto, some images have no thumbnails -- an empty dashed rectangle is displayed instead. Those JPEG images application downloads are also generated by my application as a result of processing pictures either taken by device camera or picked from Photo Library. Maybe I need to do something special during image processing that will make thumbnails visible?
Try something like
UIImage * original = [UIImage imageNamed:#"sample.jpg"]; /* make image from CGRef */
NSData * imdata = UIImagePNGRepresentation ( original ); /* get PNG representation */
UIImage * png = [UIImage imageWithData:imdata]; /* wrap UIImage around PNG representation */
UIImageWriteToSavedPhotosAlbum(png,
self,
#selector(image:didFinishSavingWithError:contextInfo:),
nil);
This will convert your image to PNG, and the thumbnail will show in Photos.app.

How to insert image in xml on iphone

In my app i insert images in the tableview by putting the url of image in the field image of xml!!
It's possible insert the entire image directly in XML, including in the field image, instead of the URL, a kind of image compression, for example, the bits that make up the image.
This will increase the loading time a lot!
Search for a solution for "lazy loading" on the Web.
This is the most common approach for loading images in the table views on iPhone...
EDIT:
If you still want to use blub (image data as text inside the XML) then you might use the next code sample:
NSData *imageData = [NSData dataWithBase64EncodedString:imageString];
UIImage *image = [UIImage imageWithData:imageData];