简体   繁体   English

使用CIFilter后图像自动旋转

[英]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. 我正在使用UISliders实现亮度/对比度/饱和度的工具,并使用Core Image Filter类来实现。 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. 但是,如果我选择编辑图片,然后使用任何描述的滑块工具,图像将逆时针旋转90度。 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. 我认为问题源于brightnessSlider方法,基于我放置的断点。 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. 这只是记录图像的原始方向和比例,并在数据转换回UIImage时重新设置它们。 Hope this helps someone else! 希望这有助于其他人!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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