2

我需要在我的代码中使用苹果的自动增强图像。我找到了苹果展示的例子,但我无法让它工作。我实际上不确定我做错了什么。

这是苹果推荐的:AutoEnhanceCode

这是我正在做的事情(imageFinal 是我想要增强的 UIImage):

- (IBAction)enhance:(id)sender {
  CIImage *myImage;
  CIImage *image = [imageFinal CIImage];
  NSDictionary *options = [NSDictionary dictionaryWithObject:[[image properties]valueForKey:kCIImageProperties] forKey:CIDetectorImageOrientation];
  NSArray *adjustments = [myImage autoAdjustmentFiltersWithOptions:options];
  for (CIFilter *filter in adjustments){
    [filter setValue:myImage forKey:kCIInputImageKey];
    myImage = filter.outputImage;
}

}

我得到的错误是:不兼容的指针类型将'const CFStringRef'(又名'const struct __CFString *const')发送到'NSString *'类型的参数

我真的不知道如何使用这个 kCGImagePropertyOrrientation。我只想应用那个简单的增强。

干杯。

4

2 回答 2

3

这是用于自动增强 UIImage 的 Swift 版本

extension UIImage {
    func autoEnhance() -> UIImage? {
        if var ciImage = CIImage.init(image: self) {
            let adjustments = ciImage.autoAdjustmentFilters()
            for filter in adjustments {
                filter.setValue(ciImage, forKey: kCIInputImageKey)
                if let outputImage = filter.outputImage {
                    ciImage = outputImage
                }
            }
            let context = CIContext.init(options: nil)
            if let cgImage = context.createCGImage(ciImage, from: ciImage.extent) {
                let finalImage = UIImage.init(cgImage: cgImage, scale: self.scale, orientation: self.imageOrientation)
                return finalImage
            }
        }
        return nil
    }
}

用法:

let autoEnhanced = image.autoEnhance() // returns optional UIImage
于 2018-12-17T21:50:24.427 回答
1

CFString 是免费桥接到 NSString 的。所以你可以直接转换它(NSString *)它会起作用。但是,我也遇到了一小段不完整的示例代码,并认为我可以对其进行一些改进......制作了一个objective-c类别来解决这个问题,首先,确保在你的代码中包含Core Image和ImageIO框架代码。

标题:

@interface UIImage (AutoEnhance)
- (UIImage *)autoEnhancedImage;
@end

执行:

#import <CoreImage/CoreImage.h>
#import <ImageIO/ImageIO.h>

@implementation UIImage (AutoEnhanced)
- (UIImage *)autoEnhancedImage
{
    CIImage *ciOriginal = self.CIImage;
    if(!ciOriginal) {
        ciOriginal = [[CIImage alloc] initWithCGImage:self.CGImage];
    }
    NSDictionary *options = @{ CIDetectorImageOrientation : @(self.CGImagePropertyOrientation)};
    NSArray *adjustments = [ciOriginal autoAdjustmentFiltersWithOptions:options];
    for (CIFilter *filter in adjustments) {
        [filter setValue:ciOriginal forKey:kCIInputImageKey];
        ciOriginal = filter.outputImage;
    }
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef cgImage = [context createCGImage:ciOriginal fromRect:ciOriginal.extent];
    UIImage *enhancedImage = [[UIImage alloc] initWithCGImage:cgImage];
    CGImageRelease(cgImage);
    return enhancedImage;
}

- (CGImagePropertyOrientation)CGImagePropertyOrientation
{
    switch (self.imageOrientation) {
        case UIImageOrientationUp:
            return kCGImagePropertyOrientationUp;
        case UIImageOrientationUpMirrored:
            return kCGImagePropertyOrientationUpMirrored;
        case UIImageOrientationDown:
            return kCGImagePropertyOrientationDown;
        case UIImageOrientationDownMirrored:
            return kCGImagePropertyOrientationDownMirrored;
        case UIImageOrientationLeftMirrored:
            return kCGImagePropertyOrientationLeftMirrored;
        case UIImageOrientationRight:
            return kCGImagePropertyOrientationRight;
        case UIImageOrientationRightMirrored:
            return kCGImagePropertyOrientationRightMirrored;
        case UIImageOrientationLeft:
            return kCGImagePropertyOrientationLeft;
    }
}
@end

然后使用它,只需:

UIImage *enhancedImage = [someUIImage autoEnhancedImage];
于 2015-03-24T18:18:07.803 回答