代码之家  ›  专栏  ›  技术社区  ›  SriTeja Chilakamarri

将图像从Heic转换为Jpeg/Jpg

  •  10
  • SriTeja Chilakamarri  · 技术社区  · 8 年前

    我有一个应用程序,用户可以上传多个图像,所有图像都将存储在服务器中,并将显示在我的iOS应用程序的web视图中。

    现在,在iOS 10之前,一切都很正常,但突然我们开始看到一些图片/图像没有显示,经过一点调试,我们发现这是由于apple(HEIC)的新图像格式引起的问题,

    我试着换回原生UIImagePicker(只拾取一个图像),图像显示为Apple,我猜当用户拾取图像时,会将图像从HEIC转换为JPG,但当我使用第三方库时,情况并非如此,因为我需要实现多个图像选取器。

    虽然我们努力在服务器端进行转换过程,以避免未更新应用程序的用户面临麻烦,但我也想看看是否有任何方法可以在我的应用程序中本地转换图像格式。

    4 回复  |  直到 8 年前
        1
  •  15
  •   marshallino16    6 年前

    在将HEIC照片上传到服务器之前,有一种将其转换为JPEG的解决方法:

    NSData *jpgImageData = UIImageJPEGRepresentation(image, 0.7);
    

    如果您使用 相位集 ,为了拥有图像对象,您需要从调用此方法 PHImageManager :

    - (PHImageRequestID)requestImageForAsset:(PHAsset *)asset targetSize:(CGSize)targetSize contentMode:(PHImageContentMode)contentMode options:(nullable PHImageRequestOptions *)options resultHandler:(void (^)(UIImage *__nullable result, NSDictionary *__nullable info))resultHandler;
    

    在服务器端,您还可以使用 this API this website 直接地

        2
  •  5
  •   Niki    6 年前

    我是这样做的,

         let newImageSize = Utility.getJpegData(imageData: imageData!, referenceUrl: referenceUrl!)
    
         /**
             - Convert heic image to jpeg format
         */
         public static func getJpegData(imageData: Data, referenceUrl: NSURL) -> Data {
             var newImageSize: Data?
             if (try? Data(contentsOf: referenceUrl as URL)) != nil
             {
                    let image: UIImage = UIImage(data: imageData)!
                    newImageSize = image.jpegData(compressionQuality: 1.0)
             }
                return newImageSize!
         }
    
        3
  •  2
  •   Leonardo Rignanese    6 年前

    在Swift 3中,给定现有HEIF pic的输入路径和保存未来JPG文件的输出路径:

    func fromHeicToJpg(heicPath: String, jpgPath: String) -> UIImage? {
            let heicImage = UIImage(named:heicPath)
            let jpgImageData = UIImageJPEGRepresentation(heicImage!, 1.0)
            FileManager.default.createFile(atPath: jpgPath, contents: jpgImageData, attributes: nil)
            let jpgImage = UIImage(named: jpgPath)
            return jpgImage
        }
    

    它返回jpgPath的UIImage,如果出现问题,则返回null。

        4
  •  0
  •   Witek Bobrowski    4 年前

    我发现现有的答案很有帮助,但我也决定发表我对这个问题解决方案的看法。希望它更加清晰和“完整”。

    此解决方案将图像保存到文件中。

    private let fileManager: FileManager
    
    func save(asset: PHAsset, to destination: URL) {
        let options = PHContentEditingInputRequestOptions()
        options.isNetworkAccessAllowed = true
    
        asset.requestContentEditingInput(with: options) { input, info in
            guard let input = input, let url = input.fullSizeImageURL else {
                return // you might want to handle this case
            }
    
            do {
                try self.save(input, at: url, to: destination)
                // success!
            } catch {
                // failure, handle the error!
            }
        }
    }
    
    private func copy(
        _ input: PHContentEditingInput, at url: URL, to destination: URL
    ) throws {
        let uniformType = input.uniformTypeIdentifier ?? ""
        switch uniformType {
        case UTType.jpeg.identifier:
            // Copy JPEG files directly
            try fileManager.copyItem(at: url, to: destination)
        default:
            // Convert HEIC/PNG and other formats to JPEG and save to file
            let image = UIImage(data: try Data(contentsOf: url))
            guard let data = image?.jpegData(compressionQuality: 1) else {
                return // you might want to handle this case
            }
            try data.write(to: destination)
        }
    }