74

我正在从第三方提供的 URL 加载图像。URL 上没有文件扩展名(或文件名)(因为它是一个模糊的 URL)。我可以从中获取数据(以 NSData 的形式)并将其加载到 UIImage 中并正常显示。

我想将此数据保存到文件中。但是,我不知道数据是什么格式(PNG、JPG、BMP)?我认为它是 JPG(因为它是来自网络的图像),但是是否有一种程序化的方式可以确定?我查看了 StackOverflow 和文档,但找不到任何东西。

TIA。


编辑:我真的需要文件扩展名吗?我将它保存到外部存储(Amazon S3),但考虑到它将始终在 iOS 或浏览器的上下文中使用(两者似乎都可以在没有扩展名的情况下解释数据)也许这不是问题.

4

11 回答 11

145

如果您有图像文件的 NSData,那么您可以通过查看第一个字节来猜测内容类型:

+ (NSString *)contentTypeForImageData:(NSData *)data {
    uint8_t c;
    [data getBytes:&c length:1];

    switch (c) {
    case 0xFF:
        return @"image/jpeg";
    case 0x89:
        return @"image/png";
    case 0x47:
        return @"image/gif";
    case 0x49:
    case 0x4D:
        return @"image/tiff";
    }
    return nil;
}
于 2011-02-18T14:20:03.017 回答
29

在wl.'s answer的基础上进行改进,这是一种基于签名预测图像 MIME 类型的更扩展和更精确的方法。该代码很大程度上受到 php 的ext/standard/image.c的启发。

- (NSString *)mimeTypeByGuessingFromData:(NSData *)data {

    char bytes[12] = {0};
    [data getBytes:&bytes length:12];

    const char bmp[2] = {'B', 'M'};
    const char gif[3] = {'G', 'I', 'F'};
    const char swf[3] = {'F', 'W', 'S'};
    const char swc[3] = {'C', 'W', 'S'};
    const char jpg[3] = {0xff, 0xd8, 0xff};
    const char psd[4] = {'8', 'B', 'P', 'S'};
    const char iff[4] = {'F', 'O', 'R', 'M'};
    const char webp[4] = {'R', 'I', 'F', 'F'};
    const char ico[4] = {0x00, 0x00, 0x01, 0x00};
    const char tif_ii[4] = {'I','I', 0x2A, 0x00};
    const char tif_mm[4] = {'M','M', 0x00, 0x2A};
    const char png[8] = {0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a};
    const char jp2[12] = {0x00, 0x00, 0x00, 0x0c, 0x6a, 0x50, 0x20, 0x20, 0x0d, 0x0a, 0x87, 0x0a};


    if (!memcmp(bytes, bmp, 2)) {
        return @"image/x-ms-bmp";
    } else if (!memcmp(bytes, gif, 3)) {
        return @"image/gif";
    } else if (!memcmp(bytes, jpg, 3)) {
        return @"image/jpeg";
    } else if (!memcmp(bytes, psd, 4)) {
        return @"image/psd";
    } else if (!memcmp(bytes, iff, 4)) {
        return @"image/iff";
    } else if (!memcmp(bytes, webp, 4)) {
        return @"image/webp";
    } else if (!memcmp(bytes, ico, 4)) {
        return @"image/vnd.microsoft.icon";
    } else if (!memcmp(bytes, tif_ii, 4) || !memcmp(bytes, tif_mm, 4)) {
        return @"image/tiff";
    } else if (!memcmp(bytes, png, 8)) {
        return @"image/png";
    } else if (!memcmp(bytes, jp2, 12)) {
        return @"image/jp2";
    }

    return @"application/octet-stream"; // default type

}

上述方法可识别以下图像类型:

  • image/x-ms-bmp(bmp)
  • image/gif(gif)
  • image/jpeg(JPG,JPEG)
  • image/psd(psd)
  • image/iff(当)
  • image/webp(网络)
  • image/vnd.microsoft.icon(ico)
  • image/tiff(提夫,提夫)
  • image/png(PNG)
  • image/jp2(JP2)

不幸的是,没有简单的方法可以从实例中获取此类信息UIImage,因为无法访问其封装的位图数据。

于 2014-12-28T18:59:23.937 回答
15

@Tai Le 针对 Swift 3 的解决方案是将整个数据分配到字节数组中。如果图像很大,可能会导致崩溃。此解决方案仅分配单个字节:

import Foundation

public extension Data {
    var fileExtension: String {
        var values = [UInt8](repeating:0, count:1)
        self.copyBytes(to: &values, count: 1)

        let ext: String
        switch (values[0]) {
        case 0xFF:
            ext = ".jpg"
        case 0x89:
            ext = ".png"
        case 0x47:
            ext = ".gif"
        case 0x49, 0x4D :
            ext = ".tiff"
        default:
            ext = ".png"
        }
        return ext
    }
}
于 2017-04-06T09:18:13.720 回答
9

如果您从 URL 中检索图像,那么您可能可以检查 HTTP 响应标头。标题是否Content-Type包含任何有用的信息?(我想它会因为浏览器可能能够正确显示图像,并且只有在内容类型设置适当的情况下才能做到这一点)

于 2010-11-10T17:44:54.943 回答
8

斯威夫特3版本:

let data: Data = UIImagePNGRepresentation(yourImage)!

extension Data {
    var format: String {
        let array = [UInt8](self)
        let ext: String
        switch (array[0]) {
        case 0xFF:
            ext = "jpg"
        case 0x89:
            ext = "png"
        case 0x47:
            ext = "gif"
        case 0x49, 0x4D :
            ext = "tiff"
        default:
            ext = "unknown"
        }
        return ext
    }
}
于 2016-10-29T08:01:32.113 回答
4

另一种接受的答案是使用 . 检查图像的 UTI image I/O frameWork。您可以实现图像类型表单 UTI。尝试这个:

CGImageSourceRef imgSrc = CGImageSourceCreateWithData((CFDataRef)data, NULL);
NSString *uti = (NSString*)CGImageSourceGetType(imgSrc);
NSLog(@"%@",uti);

例如,GIF 图像的 UTI 是“com.compuserve.gif”,PNG 图像的 UTI 是“public.png”。但是您无法从image I/O frameWork无法识别的图像中实现 UTI。

于 2017-03-29T07:32:18.990 回答
3

要从中获取图像类型,UIImage您可以从底层 Quartz 图像数据中获取类型标识符 (UTI):

extension UIImage {
    var typeIdentifier: String? {
        cgImage?.utType as String?
    }
}

要从 URL 获取图像类型标识符,这将取决于 URL 是否指向本地资源:

extension URL {
    // for local resources (fileURLs)
    var typeIdentifier: String? { (try? resourceValues(forKeys: [.typeIdentifierKey]))?.typeIdentifier }
    // for non local resources (web) you can get it asyncronously
    func asyncTypeIdentifier(completion: @escaping ((String?, Error?) -> Void)) {
        var request = URLRequest(url: self)
        request.httpMethod = "HEAD"
        URLSession.shared.dataTask(with: request) { _ , response , error in
            completion((response as? HTTPURLResponse)?.mimeType, error)
        }.resume()
    }
}

let imageURL = URL(string: "https://i.stack.imgur.com/varL9.jpg")!
imageURL.asyncTypeIdentifier { typeIdentifier, error in
    guard let typeIdentifier = typeIdentifier, error == nil else { return }
    print("typeIdentifier:", typeIdentifier)
}
于 2020-05-16T05:11:18.287 回答
3

我基于@ccoroom 改进的解决方案

//  Data+ImageContentType.swift

import Foundation

extension Data {  
    enum ImageContentType: String {
        case jpg, png, gif, tiff, unknown

        var fileExtension: String {
            return self.rawValue
        }
    }

    var imageContentType: ImageContentType {

        var values = [UInt8](repeating: 0, count: 1)

        self.copyBytes(to: &values, count: 1)

        switch (values[0]) {
        case 0xFF:
            return .jpg
        case 0x89:
            return .png
        case 0x47:
           return .gif
        case 0x49, 0x4D :
           return .tiff
        default:
            return .unknown
        }
    }
}

一些使用示例:

//load some image
do {
    let imageData = try Data(contentsOf: URL(string: "https://myServer/images/test.jpg")!)
} catch {
    print("Unable to load image: \(error)")
}

//content type check
guard [Data.ImageContentType.jpg,
       Data.ImageContentType.png].contains(imageData.imageContentType) else {
    print("unsupported image type")
            return
        }

//set file extension
let image = "myImage.\(imageData.imageContentType.fileExtension)" //myImage.jpg
于 2019-08-05T15:38:06.487 回答
1

如果这对您来说真的很重要,我相信您将不得不检查字节流。JPEG 将从字节 FF D8 开始。PNG 将以 89 50 4E 47 0D 0A 1A 0A 开头。我不知道 BMP 是否有类似的标头,但我认为您不太可能在 2010 年遇到网络上的标头。

但这对你真的很重要吗?你不能把它当作一个未知的图像,让 Cocoa Touch 来完成工作吗?

于 2010-11-10T17:45:49.823 回答
1

我创建了一个库来检查 NSData 的图像类型:

https://github.com/sweetmandm/ImageFormatInspector

于 2015-03-26T15:15:00.993 回答
1

对每种已知图像格式实施签名检查。这是一个用于 PNG 数据的快速 Objective-C 函数:

// Verify that NSData contains PNG data by checking the signature

- (BOOL) isPNGData:(NSData*)data
{
  // Verify that the PNG file signature matches

  static const
  unsigned char   png_sign[8] = {137, 80, 78, 71, 13, 10, 26, 10};

  unsigned char   sig[8] = {0, 0, 0, 0, 0, 0, 0, 0};

  if ([data length] <= 8) {
    return FALSE;
  }

  [data getBytes:&sig length:8];

  BOOL same = (memcmp(sig, png_sign, 8) == 0);

  return same;
}
于 2011-06-09T20:46:19.070 回答