使用CIFilter后图像自动旋转 [英] Image auto-rotates after using CIFilter

查看:147
本文介绍了使用CIFilter后图像自动旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个应用程序,让用户拍照,然后进行编辑。我正在使用UISliders实现亮度/对比度/饱和度的工具,并使用Core Image Filter类来实现。当我打开应用程序时,我可以拍照并正确显示。但是,如果我选择编辑图片,然后使用任何描述的滑块工具,图像将逆时针旋转90度。这是有问题的代码:

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);
}

我认为问题源于 brightnessSlider 方法,基于我放置的断点。有没有办法阻止我的照片自动旋转?如果没有,我该如何将其旋转回正常方向?

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

}

这只是记录图像的原始方向和比例,并在数据转换回UIImage时重新设置它们。希望这有助于其他人!

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!

这篇关于使用CIFilter后图像自动旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆