Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/117.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何将UIImage转换为BMP并另存为数据(不是JPG或PNG)_Ios_Swift3_Bmp - Fatal编程技术网

Ios 如何将UIImage转换为BMP并另存为数据(不是JPG或PNG)

Ios 如何将UIImage转换为BMP并另存为数据(不是JPG或PNG),ios,swift3,bmp,Ios,Swift3,Bmp,我正在iOS应用程序(Swift 3)和仅支持BMP格式的应用程序(在MS Windows上)之间同步BMP图像 在MS Windows应用程序上创建的BMP图像作为base64字符串下载,另存为数据,并使用以下代码行轻松显示: let imageData = Data(base64Encoded: value) let image = UIImage(data: imageData) 这是从下载的BMP图像生成一个UIImage,效果非常好,因为iCAS显示图像 但我还需要从iOS应用程序中

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

在MS Windows应用程序上创建的BMP图像作为base64字符串下载,另存为
数据
,并使用以下代码行轻松显示:

let imageData = Data(base64Encoded: value)
let image = UIImage(data: imageData)
这是从下载的BMP图像生成一个
UIImage
,效果非常好,因为iCAS显示图像

但我还需要从iOS应用程序中创建BMP图像(绘制的签名),为此,多亏了Felipe,我正在使用这个库

此库生成一个
UIImage
,我将其另存为
数据
,并可以在iOS应用程序上显示

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

有人能帮我从
UIImage
或直接从
SHFSignature
图像功能生成真实的BMP图像吗


谢谢

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

我在这里找到了一个有用的扩展:

我使用了这个扩展: //注意:需要导入MobileCoReserves才能识别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
            }
        }
    }
}
我在代码中这样调用它:

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

let选项:NSDictionary=[:]
让convertToBmp=self.backgroundImage.toData(选项:选项,类型:.bmp)
guard let bmpData=convertToBmp else{
打印(“