简体   繁体   中英

Image auto-rotates after using CIFilter

I am writing an app that lets users take a picture and then edit it. I am working on implementing tools with UISliders for brightness/contrast/saturation and am using the Core Image Filter class to do so. When I open the app, I can take a picture and display it correctly. However, if I choose to edit a picture, and then use any of the described slider tools, the image will rotate counterclockwise 90 degrees. Here's the code in question:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.navigationItem.hidesBackButton = YES; //hide default nav

    //get image to display
    DBConnector *dbconnector = [[DBConnector alloc] init];
    album.moments = [dbconnector getMomentsForAlbum:album.title];
    Moment *mmt = [album.moments firstObject];
    _imageView.image = [mmt.moment firstObject];

    CGImageRef aCGImage = _imageView.image.CGImage;
    CIImage *aCIImage = [CIImage imageWithCGImage:aCGImage];
    _editor = [CIFilter filterWithName:@"CIColorControls" keysAndValues:@"inputImage", aCIImage, nil];

    _context = [CIContext contextWithOptions: nil];
    [self startEditControllerFromViewController:self];

}

//cancel and finish buttons
- (BOOL) startEditControllerFromViewController: (UIViewController*) controller {

    [_cancelEdit addTarget:self action:@selector(cancelEdit:) forControlEvents:UIControlEventTouchUpInside];
    [_finishEdit addTarget:self action:@selector(finishEdit:) forControlEvents:UIControlEventTouchUpInside];

    return YES;
}

//adjust brightness
- (IBAction)brightnessSlider:(UISlider *)sender {

    [_editor setValue:[NSNumber numberWithFloat:_brightnessSlider.value] forKey: @"inputBrightness"];

    CGImageRef cgiimg = [_context createCGImage:_editor.outputImage fromRect:_editor.outputImage.extent];
    _imageView.image = [UIImage imageWithCGImage: cgiimg];
    CGImageRelease(cgiimg);
}

I believe that the problem stems from the brightnessSlider method, based on breakpoints that I've placed. Is there a way to stop the auto-rotating of my photo? If not, how can I rotate it back to the normal orientation?

Mere minutes after posting, I figured out the answer to my own question. Go figure. Anyway, I simply changed the slider method to the following:

- (IBAction)brightnessSlider:(UISlider *)sender {

    [_editor setValue:[NSNumber numberWithFloat:_brightnessSlider.value] forKey: @"inputBrightness"];

    CGImageRef cgiimg = [_context createCGImage:_editor.outputImage fromRect:_editor.outputImage.extent];
    UIImageOrientation originalOrientation = _imageView.image.imageOrientation;
    CGFloat originalScale = _imageView.image.scale;

    _imageView.image = [UIImage imageWithCGImage: cgiimg scale:originalScale orientation:originalOrientation];
    CGImageRelease(cgiimg);

}

This simply records the original orientation and scale of the image, and re-sets them when the data is converted back to a UIImage. Hope this helps someone else!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM