使用CIFilter后图像自动旋转

时间:2014-07-18 20:28:29

标签: ios uiimage uislider core-image cifilter

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

- (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方法,基于我提出的断点。有没有办法阻止我的照片自动旋转?如果没有,我该如何将其旋转回正常方向?

1 个答案:

答案 0 :(得分:5)

发布后几分钟,我找到了自己问题的答案。去搞清楚。无论如何,我只是将滑块方法更改为以下内容:

- (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时重新设置它们。希望这有助于其他人!