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
636 views
in Technique[技术] by (71.8m points)

ios - 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?

See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

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!


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

...