无法在 swift 中压缩 tiff 文件

Unable to compress tiff file in swift

我的应用程序需要使用 iPad 相机捕获纸质文档的快照并将其以 Tiff 格式传递给 REST API。

我尝试使用 "UIImagePickerController" 捕获图像并使用 CGImageDestinationRef 中的 "kUTTypeTIFF" 将其转换为 Tiff 格式。它在 ipad pro12.9 中生成大约 20 到 30 MB 的未压缩图像。

现在我正在尝试使用各种格式压缩图像,但我很困惑,因为在 swift tiff 压缩中找不到指南。

语言:Swift

拍照代码片段,

let chosenImage = info[UIImagePickerControllerOriginalImage] as! CGImage //2

let imgData = convertToTiffUsingUIImage(imgUI: info[UIImagePickerControllerOriginalImage] as! UIImage)

//Pass to REST
imageUploadRequest(imgData, uploadUrl: NSURL(string: "http://localhost:8080/user/service.jsp")!, param: ["userName": "xyz"])

self.dismissViewControllerAnimated(true, completion: nil);

转换为 tiff 图像

let key = kCGImagePropertyTIFFCompression
let value = 5

var keys = [ unsafeAddressOf(key) ]
var values = [ unsafeAddressOf(value) ]

var keyCallBacks = kCFTypeDictionaryKeyCallBacks
var valueCallBacks = kCFTypeDictionaryValueCallBacks

let cfDictionary = CFDictionaryCreate(kCFAllocatorDefault, &keys, &values, 1, &keyCallBacks, &valueCallBacks) 

let data = NSMutableData()

let dr: CGImageDestinationRef = CGImageDestinationCreateWithData(data, kUTTypeTIFF, 1, cfDictionary)!

CGImageDestinationAddImage(dr, imgUI.CGImage!, nil);

CGImageDestinationFinalize(dr);

print(data.length);

if data.length > 0 {
    UIImageWriteToSavedPhotosAlbum(UIImage(data:data)!, nil, nil, nil);
}

但这不会导致压缩图像。

谁能帮我理解 swift 中的 Tiff 文件压缩?

您不应该以 TIFF 形式传递照片。照片需要是 JPG,这是您应该使用的格式。换句话说,照片的压缩版本 JPG。不要使用 TIFF 作为中介。

(最近有少数设备支持RAW,但你不能指望它。)

这是我对 this 答案中的代码进行转换的粗略尝试。

import Cocoa
import ImageIO
import CoreServices

func writeCCITTTiffWithCGImageURL(im: CGImage, url: CFURL)
{
   guard let colorSpace = CGColorSpace(name: CGColorSpace.genericGrayGamma2_2) else { print("Color space deosn't exist"); return }
   guard let bitmapCtx = CGContext(data: nil, width: im.width, height: im.height, bitsPerComponent: 8, bytesPerRow: 0, space: colorSpace, bitmapInfo: CGImageAlphaInfo.none.rawValue) else { print("Could not make bitmapContext"); return }

   bitmapCtx.draw(im, in: CGRect(x: 0, y: 0, width: im.width, height: im.height))
   guard let grayScaleImage: CGImage = bitmapCtx.makeImage()
   else { print("Could not make grayScale Image"); return }

   let tiffOptions: Dictionary<String, Int> = [String(kCGImagePropertyTIFFCompression) : 4]
   let options: NSDictionary = [String(kCGImagePropertyTIFFDictionary) : tiffOptions, kCGImagePropertyDepth : 1]

   guard let imageDestination = CGImageDestinationCreateWithURL(url, kUTTypeTIFF, 1, nil) else { print("Could not make imageDestination"); return }
   CGImageDestinationAddImage(imageDestination, grayScaleImage, options)
   CGImageDestinationFinalize(imageDestination)
}

这是示例:

   import UIKit
    
    import ImageIO
    
    import MobileCoreServices
    
    
    static func converterImgtoTIFF(img: UIImage) -> String{
            
            let options: NSDictionary =     [:]
            let convertToTIFF = img.toData(options: options, type: .tiff)
            guard let bmpData = convertToTIFF else {
                print(" ERROR: could not convert image to a bitmap bmpData var.")
                return ""
            }
            print("---------- convertToTIFF ----------------")
            let imagTiff = convertToTIFF?.base64EncodedString(options: .lineLength64Characters) ?? ""
           
            print("convert To TIFF =>>", imagTiff.count)
            
            return imagTiff
        }


extension UIImage {
    func toData (options: NSDictionary, type: ImageType) -> Data? {
        guard cgImage != nil else { return nil }
        return toData(options: options, type: type.value)
    }

    func toData (options: NSDictionary, type: CFString) -> Data? {
        guard let cgImage = cgImage else { return nil }
        return autoreleasepool { () -> Data? in
            let data = NSMutableData()
            guard let imageDestination = CGImageDestinationCreateWithData(data as CFMutableData, type, 1, nil) else { return nil }
            CGImageDestinationAddImage(imageDestination, cgImage, options)
            CGImageDestinationFinalize(imageDestination)
            return data as Data
        }
    }

    enum ImageType {
        case tiff // TIFF image
        var value: CFString {
            switch self {
            case .tiff: return kUTTypeTIFF
            }
        }
    }
}