开发者

converting uiimage to negative colors 2nd time crashes the app

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) ge开发者_C百科tPhoto:(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?


This method will give you the inverted image of the image you specify in the parameter

-(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;
}

Use it like this:

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


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.


Fernando Cervantes' answer in 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
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜