Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
746 views
in Technique[技术] by (71.8m points)

iphone - 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) 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...