Scaling images and save them - iphone

In my app, the user can take an image through the camera or chose one from the photo album.
(I save the photo taken with the camera in the photo album).
In both cases, I would like to save a scaled copy (640x920) of the image in the documents directory.
How can I proceed to scale and then save the resulting image?

The easiest way is with NYXImagesUtilities.
Here's the github page.
A basic example of using it.
// #import <QuartzCore/QuartzCore.h>
// #import "NYXImagesUtilities.h"
//.. path is an NSString of the documents directory + file name you want to save it to.
//.. myImage is the UIImage that you got from the user.
UIImage *scaledImage = [myImage scaleToFitSize:(CGSize){640, 920}];
[scaledImage saveToPath:path type:NYXImageTypePNG];

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.

How to obtain a copied image from the pasteboard?

I have a photo editing app where I want to provide a way for the user to edit photos from the pasteboard. For example the user might copy a photo from his website in Safari and then head over to my app and choose "Open from pasteboard". Kind of like Photoshop does it.
How can I get notified or check if there is a picture or graphic (probably a UIImage) in the pasteboard? And how could I obtain that image?
I needed one for a UIImage and this snippet helped me out.
You can always extract the image out using:
UIImage *image = [UIPasteboard generalPasteboard].image;

Save iPhone Photo Library Path to a Plist

I want to save an image path string for a particular photo from the main iphone photo library in a plist file.
So that later I can call the plist image path string in a web view and see the photo.
I know I need the absolute path which is probably a similar path to the documents directory but:
1.) How do I get the photo name of the particular photo?
2.) Do I need to append the absolute path string to the photo name and then write to the plist?
3.) Will the path change for every install of my app - which would make a hard coded absolute path impossible to use?
Any pros want to tackle this conundrum?
If you are building an app targeting lower than iOS4.0, then you may use UIImagePickerController Class. This will let user pick one image from the phone album, which then, you can save in your app's documents directory and save the corresponding link in the info.plist file. The documents directory gets deleted when your app gets uninstalled, and so you lose the path as well.
A better way, with a disadvantage of restricting this feature (and so your app if its a main feature) to only iOS4.0 and above, would be using ALAssetsLibrary. Each photo in the iphone is represented by a unique URL that you can save, and you can refer to a particular image using that url. There is a nice tutorial blog that can help you get started with it.
Now, in your question, you are talking about a particular photo. You can not just know of any particular photo in the library, unless you let the user pick one. This brings me to answer your last question. The URL is constant for that particular image (till the iphone is reset), but unless you have any way of keeping that information between every install of your app (what exactly do mean here?), I don't know how being a unique URL is going to help you here.
EDIT:
Just saw the tag of sdk 4.0 there, please ignore the first case (the <4.0 part).
OK I think I want to use the UIImagePickerController!
Here is my code:
- (void)viewDidLoad {
self.iconPicker = [[UIImagePickerController alloc] init];
self.iconPicker.allowsEditing = YES;
self.iconPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
self.iconPicker.delegate = self;
}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
iconImage.image = [info objectForKey:UIImagePickerControllerEditedImage];
[picker dismissModalViewControllerAnimated:YES];
[picker release];
}
- (IBAction)save {
[plist.data setValue:iconImage.image forKey:#"Icon"];
[plist.data writeToFile:plist.file atomically:YES];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
How do I save the image name of the edited image in plist string.
I know I don't want to save the CGImage data or whatever because of the load size.
I think I can pass an xcode variable into HTML through putting all my HTML into string objects and then adding variables to the objects where I need includes...
So I can create a path to the photo directory but I need to know how to get the name of the saved edited image from UIImagePickerController???

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