converting uiimage to negative colors 2nd time cra

2019-01-27 03:51发布

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?

3条回答
走好不送
2楼-- · 2019-01-27 04:08

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];
查看更多
祖国的老花朵
3楼-- · 2019-01-27 04:19

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.

查看更多
冷血范
4楼-- · 2019-01-27 04:20

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
}
查看更多
登录 后发表回答