【问题标题】:Unable to compress tiff file in swift无法快速压缩 tiff 文件
【发布时间】:2017-02-01 15:34:23
【问题描述】:

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

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

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

语言:斯威夫特

拍照代码sn-p,

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);
}

但这不会产生压缩图像。

谁能帮我快速理解 Tiff 文件压缩?

【问题讨论】:

  • AFAIK 您不能压缩 tiff 图像本身(tiff 未压缩,API 需要这种格式很奇怪),您可以做的是压缩(或 targz)文件并上传该文件, API 可能不会接受它。
  • @HAS 压缩 tiff 是一个东西,但 Cocoa 不支持它 AFAIK。
  • 谢谢@matt,我不知道,所以我一直纠正:)

标签: ios swift tiff cgimage


【解决方案1】:

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

(最近有少数设备支持 RAW,但您不能指望这一点。)

【讨论】:

    【解决方案2】:

    这是我在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)
    }
    

    【讨论】:

      【解决方案3】:

      这是一个例子:

         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
                  }
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-01-22
        • 1970-01-01
        • 1970-01-01
        • 2017-05-14
        • 2011-07-30
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多