UIImageWriteToSavedPhotosAlbum saves to wrong size and quality - iphone

I'm trying to take a screen shot of my app's current view and save it to photo album (to then be emailed or MMS'ed).
UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImageWriteToSavedPhotosAlbum(viewImage, self, #selector(savedPhotoImage:didFinishSavingWithError:contextInfo:), nil);
This works but the resulting image apears to be larger (533x800px) and heavily compressed when I email it from the photo library.
I've tried first writing the UIImage to file and then saving to album but still get the same issue.
If I used the in-built screenshot functionality on the iPhone the view saves correctly to photo album at 320x480 but the above code appears to save a larger image for some reason?
Thanks!

I found a decent workaround, which is to essentially rewrap the UIImage as a PNG, then save the rewrapped version. Code looks something like 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

I had that same error, on my part, that was solved when I rounded the decimal points to be the same scale as the iPhone, try it, make sure the scale is 1.0, 2.0, etc and not 3.1, that will throw it off.

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.

Load image from a stored png

How does one load or fetch an image that was saved to disk?
I wrote the image to the documents filepath using NSData and a PNG representation. How does one get this image back? UIImage.
UIImage *theImage = [UIImage imageWithContentsOfFile:filePath];
As ever, see the UIImage documentation at Apple. It is your friend.

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;

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.

iphone : How to convert webpage(UIWebView) to pdf

i want to convert my webpage(displayed in UIWebView) contents in pdf file.
which can be stored at perticular directory (document dir/any user defined dir).
So later on it can be viewable to user either internet connection not available.
is there any possibilities to perform this functionality.
how can i do this...
Thanking you in advance...
An image might be your best option. See Take snapshot of view / WebView programmatically
To get the image, you'll want to use:
UIGraphicsBeginImageContext(self.bounds.size);
[theView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
Then, to save to the Photos Library:
UIImageWriteToSavedPhotosAlbum(viewImage,nil,NULL,NULL);