I had a super noob question about assigning objects in objective-c. I basically am trying to create an array of thumbnails to use. I have this code from another post.
+ (UIImage *)scale:(UIImage *)image toSize:(CGSize)size
{
UIGraphicsBeginImageContext(size);
[image drawInRect:CGRectMake(0, 0, size.width, size.height)];
UIImage *scaledImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return scaledImage;
}
In my viewDidLoad, I do this:
// Create thumbnail images to be used in UITableView.
NSMutableArray *imageArray = [[NSMutableArray alloc] initWithCapacity:[_list count]]; // list is an array that has my data
for (id *test in _list) {
UIImage *cardImage = [UIImage imageNamed:test.ImageFile]; // line 1
cardImage = [UIImage scale:cardImage toSize:CGSizeMake(50, 50)]; // line 2
[imageArray addObject:cardImage]; // line 3
}
self.List = imageArray;
[imageArray release];
My question is in regards to inside the for loop. This is my understanding of how it works, please correct me if I’m wrong.
Line 1 creates an autorelease UIImage called cardImage.
Line 2 creates another UIImage from the scale:toSize: method. (I’m not sure if this UIImage is autoreleased or not). cardImage now points to this new UIImage created in line 2 and the autoreleased UIImage from line 1 will later be cleaned up.
Line 3 adds that new object to imageArray.
I wasn’t sure if this was right/wrong/good/bad, etc. I know with primitive types in a method, you can do
int x = 5;
x = 10;
But I wasn’t sure if I was creating a memory leak here by doing this, and if there was a better method. Thanks a bunch!
UIGraphicsGetImageFromCurrentImageContext returns an autoreleased image so does
scale:toSize:. Therefore, your loop does not leak.Your first clue could have been the name:
scale:toSize:. According to the Memory Management Rules, only methods whose names begin with “alloc”, “new”, “copy” or “mutableCopy” return objects you own. Other methods return objects you do not own.