给定CIImage,将图像数据写入磁盘的最快方法是什么?

Jor*_*n H 5 uiimagejpegrepresentation ios ciimage swift photokit

我正在使用PhotoKit,并已实现过滤器用户可以应用于照片库中的照片.我目前正在获取图像,应用过滤器,将编辑后的版本作为a返回CIImage,然后我将其转换CIImageNSData使用,UIImageJPEGRepresentation因此我可以将其写入磁盘.虽然这种方法效果很好,但当用户尝试编辑非常大(如30 MB)的照片时,可能需要花费30秒才能完成此操作,其中98%的时间用于UIImageJPEGRepresentation(从乐器获得的统计数据).

如果可能的话,我正在寻找一种更有效的方法将此编辑过的照片保存到磁盘而不会影响质量.

我理解UIImagePNGRepresentation可能会提高质量,但这比JPEG表示更慢.

这是我目前正在做的,在默认优先级线程(qos_class_utility)上:

func jpegRepresentationOfImage(image: CIImage) -> NSData {
    let eaglContext = EAGLContext(API: .OpenGLES2)
    let ciContext = CIContext(EAGLContext: eaglContext)

    let outputImageRef = ciContext.createCGImage(image, fromRect: image.extent())
    let uiImage = UIImage(CGImage: outputImageRef, scale: 1.0, orientation: UIImageOrientation.Up)

    return UIImageJPEGRepresentation(uiImage, 0.9) //this takes upwards of 20-30 seconds with large photos!
}

//writing out to disk:
var error: NSError?
let success = jpegData.writeToURL(contentEditingOutput.renderedContentURL, options: NSDataWritingOptions.AtomicWrite, error: &error)
Run Code Online (Sandbox Code Playgroud)

Joe*_*aro 10

尝试使用如下所示的 CIContext 方法writeJPEGRepresentation(适用于 iOS 10)来避开 UIImage 并使写入速度更快。

extension CIImage {

    @objc func saveJPEG(_ name:String, inDirectoryURL:URL? = nil, quality:CGFloat = 1.0) -> String? {
        
        var destinationURL = inDirectoryURL
        
        if destinationURL == nil {
            destinationURL = try? FileManager.default.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: true)
        }
        
        if var destinationURL = destinationURL {
            
            destinationURL = destinationURL.appendingPathComponent(name)
            
            if let colorSpace = CGColorSpace(name: CGColorSpace.sRGB) {
                
                do {

                    let context = CIContext()

                    try context.writeJPEGRepresentation(of: self, to: destinationURL, colorSpace: colorSpace, options: [kCGImageDestinationLossyCompressionQuality as CIImageRepresentationOption : quality])
                    
                    return destinationURL.path
                    
                } catch {
                    return nil
                }
            }
        }
        
        return nil
    }
}
Run Code Online (Sandbox Code Playgroud)

@objc 关键字使您能够调用 Objective-C 中的方法,如下所示:

NSString* path = [image saveJPEG:@"image.jpg" inDirectoryURL:url quality:1.0];
Run Code Online (Sandbox Code Playgroud)

对于 PNG ,iOS 11有一个类似的writePNGRepresentation方法:

   if #available(iOS 11.0, *) {

        if let colorSpace = CGColorSpace(name: CGColorSpace.sRGB) {
                
            do {
                let format = CIFormat.RGBA8
                    
                try context.writePNGRepresentation(of: self, to: destinationURL, format: format, colorSpace: colorSpace)
                    
                return destinationURL.path
                    
            } catch {
                return nil
            }   
        }
    }
Run Code Online (Sandbox Code Playgroud)


Dav*_*ard 6

我建议使用CGImageDestination将CGImage直接传递给ImageIO.您可以将字典传递给CGImageDestinationAddImage以指示压缩质量,图像方向等.

CFDataRef save_cgimage_to_jpeg (CGImageRef image)
{
    CFMutableDataRef cfdata = CFDataCreateMutable(nil,0);
    CGImageDestinationRef dest = CGImageDestinationCreateWithData(data, CFSTR("public.jpeg"), 1, NULL);
    CGImageDestinationAddImage(dest, image, NULL);
    if(!CGImageDestinationFinalize(dest))
        ; // error
    CFRelease(dest);
    return cfdata
}
Run Code Online (Sandbox Code Playgroud)