第二次将 uiimage 转换为负色会使应用程序崩溃 [英] converting uiimage to negative colors 2nd time crashes the app

查看:10
本文介绍了第二次将 uiimage 转换为负色会使应用程序崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想展示一个带有负片图像和正常图像的动画.首先,调用 getPhoto 方法,用户可以从库中获取图像或使用相机拍摄.

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 显示使用率为 100%.我怎样才能防止这种情况发生?

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

推荐答案

你释放图像两次.UIGraphicsGetImageFromCurrentImageContext() 返回一个自动释放的对象,所以不要在离开函数之前释放它.当执行返回到运行循环时,自动释放池将为您执行此操作.

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.

这篇关于第二次将 uiimage 转换为负色会使应用程序崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆