美文网首页
SDWebImage (5.1.0)NSData+ImageCo

SDWebImage (5.1.0)NSData+ImageCo

作者: charlotte2018 | 来源:发表于2019-08-19 18:07 被阅读0次

    (1)+ (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data;
    通过传入的image的data数据解析出是什么格式的图片。

    
    + (SDImageFormat)sd_imageFormatForImageData:(nullable NSData *)data {
        if (!data) {
            return SDImageFormatUndefined;
        }
        
        // File signatures table: http://www.garykessler.net/library/file_sigs.html
        uint8_t c;
        [data getBytes:&c length:1];
        switch (c) {
            case 0xFF:
                return SDImageFormatJPEG;
            case 0x89:
                return SDImageFormatPNG;
            case 0x47:
                return SDImageFormatGIF;
            case 0x49:
            case 0x4D:
                return SDImageFormatTIFF;
            case 0x52: {
                if (data.length >= 12) {
                    //RIFF....WEBP
                    NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
                    if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                        return SDImageFormatWebP;
                    }
                }
                break;
            }
            case 0x00: {
                if (data.length >= 12) {
                    //....ftypheic ....ftypheix ....ftyphevc ....ftyphevx
                    NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(4, 8)] encoding:NSASCIIStringEncoding];
                    if ([testString isEqualToString:@"ftypheic"]
                        || [testString isEqualToString:@"ftypheix"]
                        || [testString isEqualToString:@"ftyphevc"]
                        || [testString isEqualToString:@"ftyphevx"]) {
                        return SDImageFormatHEIC;
                    }
                    //....ftypmif1 ....ftypmsf1
                    if ([testString isEqualToString:@"ftypmif1"] || [testString isEqualToString:@"ftypmsf1"]) {
                        return SDImageFormatHEIF;
                    }
                }
                break;
            }
        }
        return SDImageFormatUndefined;
    }
    
    
    • 文件头规范
      JPEG (jpg),文件头:FFD8FFE1
      PNG (png),文件头:89504E47
      GIF (gif),文件头:47494638
      TIFF tif;tiff 0x49492A00
      TIFF tif;tiff 0x4D4D002A
      WebP : 524946462A73010057454250

    首先拿出data的第一个字节的东西,然后去判断是什么类型。这还有个链接,http://www.garykessler.net/library/file_sigs.html关于文件前缀类型的

    下面俩方法就是枚举和字符串互相转换的

    • (nonnull CFStringRef)sd_UTTypeFromImageFormat:(SDImageFormat)format;

    • (SDImageFormat)sd_imageFormatFromUTType:(nonnull CFStringRef)uttype;

    相关文章

      网友评论

          本文标题:SDWebImage (5.1.0)NSData+ImageCo

          本文链接:https://www.haomeiwen.com/subject/fqeesctx.html