iOS : How to handle images? - iphone

I'm making a photo managing app. I have created a class called photo, which has the name of the photo, notes on the photo, and the image. I know how to set the name and notes - they're both NSStrings, but what data type should I use for my image?
At the moment, I'm using NSData, is that correct?
Edit
Apparently, NSData is right. So, how should I get an image from a UIImage into the NSData object and back again?

Data from UIImage:
NSData *imageData = UIImagePNGRepresentation(image);
Alternatively, UIImage from data:
UIImage *image = [UIImage imageWithData:imageData];

You can store your image as an UIImage or NSData
You can simply get one from another:
NSData *data = UIImagePNGRepresentation(image);
UIImage *image=[UIImage imageWithData:data];

In case you need the image for presenting it's better practice to hold an UIImage instance in youre object transferring it to/from NSData when you need to save it or get it either from the net or the disk. If youre case is more like file manager then photo viewer, then this answer is not aplicable. Anyway the transfering routines were provided to you by previous answer.

Related

Append url to image path NSString?

I'm developing an iOS application consisting of a static sqlite database, series of tableviews and tab based detail view where the first view loaded in the detailview is a swipable imageView which loads a series of images.
I've got it working with this code where it looks for the image locally but I'd like to have it load the image from a URL or display a default image if no internet connection is available.
The images are named in the database as (for the example) image.jpg and I'd like all of them to load from the same URL directory (for the example) http://www.someurl.com/images/
Thank you
- (UIImage *) imageAtIndex:(NSUInteger)index {
Image *currentImage = (Image *) [self.images objectAtIndex:index];
NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:#"%#",[[currentImage filename] stringByDeletingPathExtension]] ofType:#"jpg"];
return [UIImage imageWithContentsOfFile:path];
}
The process for this is really very simple:
Try to download your image data using NSURLConnection
If successful, create a UIImage from the data
If either of the above fails, switch over to your placeholder/built-in image
Display the resulting image
I advise you have some sort of placeholder while the image is downloading, since that can take quite a while.
Don't bother with reachability; it's not 100% reliable, whereas actually trying the download is.
Avoid doing this on a background thread or queue. NSURLConnection is asynchronous out of the box to make this easier for you. There are a ton of third-party frameworks that try to simplify working with connections if you wish, though.
NSString *urlStr = [#"http://www.someurl.com/images/" stringByAppendingString:[currentImage filename]];
NSURL *url = [NSURL URLWithString:urlStr];
NSData *imageData = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
Or something like that...
If the image is large or the internet connection is slow, the NSData initialization might take some time. Consider getting the images data in a background thread, or use some existing framework for that like SDWebImage

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.

iphone image loading problem

I have make a one small image and in which one detail page is there.
and I in detail page I have one image tag which is in webview with html page.
It's work completely but image take a much time for loading and show in the screen.
So how can I reduce the time of loading for image. and this image is come form url.
imgBiteSpot.clipsToBounds=YES;
NSData *imageData = [[[NSData alloc]init]autorelease];
imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:ObjBitSpot.StrImagePath]];
if(imageData==0)
{
imgBiteSpot.image=[UIImage imageNamed:#"img-not-found.gif"];
}
else {
UIImage *imgs = [[UIImage alloc] initWithData:imageData];
UIGraphicsBeginImageContext(CGSizeMake(88,88));
[imgs drawInRect:CGRectMake(0.0, 0.0, 88.0, 88.0)];
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
imgBiteSpot.image=newImage;
[imgs release];
}
You could consider not downloading the image but downloading a one characters variable/flag from a web service. Depending on the answer from the web service you could load an image locally from the bundle? This could be faster.
Chris.
This is best example for loading image.
http://www.dimzzy.com/blog/2009/11/remote-image-for-iphone/
Its take time base on image size and internet speed.
But one thing you can do in your application.
You can download asynchronously that image and store it in temporary variable before you go in detail image view. When you go on that detail image you can load that downloaded image as per your requirement.

problem with Image in custom Cell

Getting image data from sqlite database but not able to display in custom cell of imageview.is there any conversion of image data into image.
Super easy. There's a UIImage convenience method for exactly that.
//assuming the existence of an NSData *myData
UIImage *myImage = [UIImage imageWithData:myData];
So easy, in fact, I suspect that's not actually your question. If not, please clarify what you're asking.

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