views:

177

answers:

2

I'm getting images from UIGetScreenImage() and storing directly in mutable array like:-

image = [UIImage imageWithScreenContents];
[array addObject:image];
[image release];

I've set this code in timer so I cant use UIImagePNGRepresentation() to store as NSData as it reduces the performance. I want to use this array directly after sometime i.e after capturing 1000 images in 100 seconds. When I use the code below:-

UIImage *im = [[UIImage alloc] init];
im = [array objectAtIndex:i];
UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);**

the application crashes. And I dont want to use UIImagePNG or JPGRepresentation() in timer as it reduces performance.

My problem is how to use this array so that it is converted into image. If anybody has idea related to it please share with me.

+1  A: 

You don't need to release the image in your first code sample there. [UIImage imageWithScreenContents] returns an autoreleased object.

Carl Norum
Thanks for the reply.[UIImage imageWithScreenContents] returns an image and in this function CGImageRef is taken from UIGetScreenImage() and it is released. so thats why image is released im first code.
sujyanarayan
+1  A: 
1. UIImage *im = [[UIImage alloc] init]; 
2. im = [array objectAtIndex:i];
3. UIImageWriteToSavedPhotosAlbum(im, nil, nil, nil);

Line 1 allocates and initializes a new UIImage object, which never gets released since you overwrite the pointer on line 2. You are leaking an UIImage in each iteration, and you don't even need to init/alloc a new object.

UIImageWriteToSavedPhotosAlbum([array objectAtIndex:i], nil, nil, nil);

Should work just fine.

Also note Carl Norum answer about releasing an autoreleased object.

arul
Also correct - I didn't even read on to see that. This error causes a memory leak, not the crash the OP is seeing, but it's worth an upvote. +1.
Carl Norum
UIImageWriteToSavedPhotosAlbum([array objectAtIndex:i], nil, nil, nil);also crashes. Actually what array contains? Is there any way to get Image from that array - Das Sujyanarayan
sujyanarayan