【问题标题】:converting uiimage to negative colors 2nd time crashes the app第二次将 uiimage 转换为负色会使应用程序崩溃
【发布时间】:2010-07-14 10:30:20
【问题描述】:

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

当动画运行时,我希望能够将图像设置为新图像,但随后应用程序崩溃了。

-(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%。 我怎样才能防止这种情况发生?

【问题讨论】:

  • 请格式化您的代码以提高可读性。代码行之间的换行会很有帮助。
  • “应用程序崩溃”出现什么错误?
  • With:wait_fences:未能收到回复:10004003 程序收到信号:“EXC_BAD_ACCESS”。
  • 只是一个猜测,但你的代码是线程化的吗?
  • 不,穿这个更好吗?

标签: iphone colors crash uiimage


【解决方案1】:

这个方法会给你在参数中指定的图像的反转图像

-(UIImage *) getImageWithInvertedPixelsOfImage:(UIImage *)image {
    UIGraphicsBeginImageContextWithOptions(image.size, NO, 2);
    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 * result = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return result;
}

像这样使用它:

yourImage.image = [self getImageWithInvertedPixelsOfImage:yourImage.image];

【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 谢谢!我还删除了动画图像,现在它可以工作了:-)
    【解决方案3】:

    费尔南多·塞万提斯在 Swift 2 中的回答:

    func getImageWithInvertedPixelsOfImage(image: UIImage) -> UIImage {
        let rect = CGRect(origin: CGPoint(), size: image.size)
    
        UIGraphicsBeginImageContextWithOptions(image.size, false, 2.0)
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), .Copy)
        image.drawInRect(rect)
        CGContextSetBlendMode(UIGraphicsGetCurrentContext(), .Difference)
        CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), UIColor.whiteColor().CGColor)
        CGContextFillRect(UIGraphicsGetCurrentContext(), rect)
    
        let result = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return result
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多