ios的AssetsLibrary PHAssetLibrary 通过图片data获取对应的图片类型.md

判断当前图片资源是不是gif

 

iOS9以上
PHAsset *asset;
[[asset valueForKey:@"filename"] containsString:@"GIF"]

 

这些类型只能通过 Asset中的 获取资源图片的详细资源信息 ios 4-9

ALAssetRepresentation *representation = [asset defaultRepresentation];
[representation filename];

通过图片data获取对应的图片类型

+ (NSString *)sd_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";
        case 0x52:
            if ([data length] < 12) {
                return nil;
            }

            NSString *testString = [[NSString alloc] initWithData:[data subdataWithRange:NSMakeRange(0, 12)] encoding:NSASCIIStringEncoding];
            if ([testString hasPrefix:@"RIFF"] && [testString hasSuffix:@"WEBP"]) {
                return @"image/webp";
            }

            return nil;
    }
    return nil;
}

 自己写的测试例子 https://github.com/qq50032660/CustomAlbumLibraryTest

 

你可能感兴趣的:(iOS)