views:

667

answers:

2

Actually, I wanted a custom cell which contains 2 image objects and 1 text object, and I decided to make a container for those objects.

So is it possible to hold a image in object and insert that object in any of the collection objects, and later use that object to display inside cell?

Thanks in advance.

+1  A: 

There should be no problem with that. Just make sure you are properly retaining it and what not in your class.

Dre
Yes, when you add an object to a collection, the collection will retain the object. The object will be released when removed from the collection - or when the collectiion is deallocated.
Chris Lundie
Just make sure you autorelease your object before adding to the collection.
lajos
+2  A: 

NSArray and NSDictionary both hold objects. These are most likely the collections you'll use with a table view.

The best way to implement what you are trying to do is to use the UIImage class. UIImages wrap a CGImage and do all the memory management for you (if your app is running low on memory, the image data is purged and automatically reloaded when you draw it- pretty cool, huh?) You can also read images from files very easily using this class (a whole bunch of formats supported).

Look at the documentation for NSArray, NSMutableArray, and UIImage for more information.

//create a UIImage from a jpeg image
UIImage *myImage = [UIImage imageWithContentsOfFile:@"myImage.jpg"];
NSArray *myArray = [NSMutableArray array];      // this will autorelease, so if you need to keep it around, retain it
[myArray addObject:myImage];



//to draw this image in a UIView's drawRect method
CGContextRef context = UIGraphicsGetCurrentContext();  // you need to find the context to draw into
UIImage *myImage = [myArray lastObject];   // this gets the last object from an array, use the objectAtIndex: method to get a an object with a specific index
CGImageRef *myCGImage = [myImage CGImage];
CGContextDrawImage(context, rect, myCGImage);  //rect is passed to drawRect
lajos