Todd Davies
Todd Davies

Reputation: 5522

iOS : How to handle images?

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?

Upvotes: 1

Views: 596

Answers (3)

Ariel
Ariel

Reputation: 2440

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.

Upvotes: 0

Stavash
Stavash

Reputation: 14304

Data from UIImage:

NSData *imageData = UIImagePNGRepresentation(image);

Alternatively, UIImage from data:

UIImage *image = [UIImage imageWithData:imageData];

Upvotes: 3

Bogdan Andresyuk
Bogdan Andresyuk

Reputation: 523

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

Upvotes: 1

Related Questions