【问题标题】:How to convert UIImage to BMP and save as Data (not JPG or PNG)如何将 UIImage 转换为 BMP 并保存为数据(不是 JPG 或 PNG)
【发布时间】:2017-06-11 17:04:54
【问题描述】:

我正在我的 iOS 应用 (Swift 3) 和仅支持 BMP 格式的应用(在 MS Windows 上)之间同步 BMP 图像。

在 MS Windows 应用程序上创建的 BMP 图像被下载为 base64 字符串,保存为 Data 并使用以下代码行很容易地显示:

let imageData = Data(base64Encoded: value)
let image = UIImage(data: imageData)

这是从下载的 BMP 图像生成UIImage 并且工作完美,因为我可以显示图像。

但我还需要从 iOS 应用程序创建 BMP 图像(绘制的签名),为此,我正在使用这个库 https://github.com/felipesantolim/SHFSignature,感谢 Felipe。

这个库生成一个UIImage,我将它保存为Data,我可以在我的iOS应用上显示它。

但是当我将它作为 base64 字符串发送时,问题是 se MS Windows 应用程序无法显示它(不支持格式,仅支持 BMP 图像)。

谁能帮我从UIImage 或直接从SHFSignature 图像函数生成真实的BMP 图像?

谢谢

【问题讨论】:

    标签: ios swift3 bmp


    【解决方案1】:

    我需要做同样的事情 - 从 iOS 获取 UIImage 并将其保存到可用的 Windows .bmp 文件中。我实际上是在 Adafruit PyPortal IoT 设备上使用它,而不是在 Windows 机器上,但下面的技术运行良好。将数据保存到 Firebase 存储并能够在 PyPortal 中使用。

    我在这里找到了一个有用的扩展: Convert UIImage to NSData and convert back to UIImage in Swift?

    我使用了这个扩展: // 请注意,您需要导入 MobileCoreServices 才能识别 k 值

    import MobileCoreServices
    
    extension UIImage {
    
        func toJpegData (compressionQuality: CGFloat, hasAlpha: Bool = true, orientation: Int = 6) -> Data? {
            guard cgImage != nil else { return nil }
            let options: NSDictionary =     [
                kCGImagePropertyOrientation: orientation,
                kCGImagePropertyHasAlpha: hasAlpha,
                kCGImageDestinationLossyCompressionQuality: compressionQuality
            ]
            return toData(options: options, type: .jpeg)
        }
    
        func toData (options: NSDictionary, type: ImageType) -> Data? {
            guard cgImage != nil else { return nil }
            return toData(options: options, type: type.value)
        }
    
        // about properties: https://developer.apple.com/documentation/imageio/1464962-cgimagedestinationaddimage
        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
            }
        }
    
        // https://developer.apple.com/documentation/mobilecoreservices/uttype/uti_image_content_types
        enum ImageType {
            case image // abstract image data
            case jpeg                       // JPEG image
            case jpeg2000                   // JPEG-2000 image
            case tiff                       // TIFF image
            case pict                       // Quickdraw PICT format
            case gif                        // GIF image
            case png                        // PNG image
            case quickTimeImage             // QuickTime image format (OSType 'qtif')
            case appleICNS                  // Apple icon data
            case bmp                        // Windows bitmap
            case ico                        // Windows icon data
            case rawImage                   // base type for raw image data (.raw)
            case scalableVectorGraphics     // SVG image
            case livePhoto                  // Live Photo
    
            var value: CFString {
                switch self {
                case .image: return kUTTypeImage
                case .jpeg: return kUTTypeJPEG
                case .jpeg2000: return kUTTypeJPEG2000
                case .tiff: return kUTTypeTIFF
                case .pict: return kUTTypePICT
                case .gif: return kUTTypeGIF
                case .png: return kUTTypePNG
                case .quickTimeImage: return kUTTypeQuickTimeImage
                case .appleICNS: return kUTTypeAppleICNS
                case .bmp: return kUTTypeBMP
                case .ico: return kUTTypeICO
                case .rawImage: return kUTTypeRawImage
                case .scalableVectorGraphics: return kUTTypeScalableVectorGraphics
                case .livePhoto: return kUTTypeLivePhoto
                }
            }
        }
    }
    

    我在我的代码中这样称呼它:

    (我正在转换的图像是一个名为 backgroundImageUIImage 值,它是下面代码所在类的属性)。

    let options: NSDictionary =     [:]
    let convertToBmp = self.backgroundImage.toData(options: options, type: .bmp)
    guard let bmpData = convertToBmp else {
        print("? ERROR: could not convert image to a bitmap bmpData var.")
        return
    }
    

    干杯!

    【讨论】:

      猜你喜欢
      • 2012-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-13
      • 2018-08-27
      • 2015-01-31
      • 2021-10-14
      相关资源
      最近更新 更多