views:

83

answers:

1

Hi!

I want show an animation with a negative image and a normal image. To start, the getPhoto method is called and the user can get an image from library or take one with the camera. When the animation is running I want to be able to set the image to a new one, but then the app crashes.

-(IBAction) getPhoto:(id) sender {

    UIImagePickerController * picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    if((UIButton *) sender == choosePhotoBtn) {
        picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    } else {
        picker.sourceType = UIImagePickerControllerSourceTypeCamera;
    }

    [self presentModalViewController:picker animated:YES];

    [picker release];

}
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

[picker dismissModalViewControllerAnimated:YES];

UIImage *negative = [self convertImageToNegative:[info objectForKey:@"UIImagePickerControllerOriginalImage"]];

imageView.animationImages = [NSArray arrayWithObjects: negative,[info objectForKey:@"UIImagePickerControllerOriginalImage"],nil];

imageView.animationDuration = 60.0; // seconds
imageView.animationRepeatCount = 1; // 0 = loops forever
[imageView startAnimating]; 

[negative release];
}

- (UIImage *)convertImageToNegative:(UIImage *)image{

UIGraphicsBeginImageContext(image.size);

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);

[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];

CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);

CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);

CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, image.size.width, image.size.height));

UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();    

return returnImage;
}

Instruments - CPU Sampler shows that the usage is 100%. How can i prevent this?

Thanks

A: 

You release the image twice. UIGraphicsGetImageFromCurrentImageContext() returns an autoreleased object, so don't release it just before leaving the function. The autorelease pool will do that for you when execution returns to the run loop.

mvds
Thank you! I also dropped animationImages, and now it works :-)