【问题标题】:Auto Enhancing Images自动增强图像
【发布时间】:2013-08-15 19:54:45
【问题描述】:

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

这是苹果推荐的: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。我只想应用那个简单的增强。

干杯。

【问题讨论】:

  • 请将您的代码图片替换为实际的代码文本。它会更容易阅读和复制。
  • 我想放上实际的屏幕截图,以便有人可以看到下划线的内容。干杯

标签: ios image-processing uiimage core-image


【解决方案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];

【讨论】:

  • 它可以工作,但由于某种原因它永远不会释放内存。使用此代码后,我的内存增加了大约 50-60 mb。而且即使释放也没有任何效果。
  • @Hassy - 如果你打开了代码,通过上面的工具检查是否有泄漏。我的猜测是其中一个 c 对象已分配但未释放。
  • 同样,大量泄漏,即使使用自动释放池
  • @Xys - 我问过同样的事情,检查它是否存在泄漏,并在您认为找到修复程序时编辑代码。只有在这里创建的东西是手动释放的cgImage(create),在ARC下自动释放的ciOriginal和enhancedImage(alloc):/
【解决方案2】:

这是一个用于自动增强 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

【讨论】:

    猜你喜欢
    • 2012-12-21
    • 2018-12-20
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 2019-05-27
    • 2011-06-03
    • 1970-01-01
    相关资源
    最近更新 更多