iOS ImageIO库使用

2,454 阅读2分钟

支持的图片类型

    CFArrayRef mySourceTypes = CGImageSourceCopyTypeIdentifiers();
    CFShow(mySourceTypes);
    CFArrayRef myDestinationTypes = CGImageDestinationCopyTypeIdentifiers();
    CFShow(myDestinationTypes);
    NSArray *sourcetype = (__bridge NSArray *)mySourceTypes;
    CFRelease(mySourceTypes);
    CFRelease(myDestinationTypes);

打印结果,测试机型 iOS 10.3.3,iPhone 5

(
    "public.jpeg",
    "public.png",
    "com.compuserve.gif",
    "com.canon.tif-raw-image",
    "com.adobe.raw-image",
    "com.dxo.raw-image",
    "com.canon.cr2-raw-image",
    "com.leafamerica.raw-image",
    "com.hasselblad.fff-raw-image",
    "com.hasselblad.3fr-raw-image",
    "com.nikon.raw-image",
    "com.nikon.nrw-raw-image",
    "com.pentax.raw-image",
    "com.samsung.raw-image",
    "com.sony.raw-image",
    "com.sony.sr2-raw-image",
    "com.sony.arw-raw-image",
    "com.epson.raw-image",
    "com.kodak.raw-image",
    "public.tiff",
    "com.canon.crw-raw-image",
    "com.fuji.raw-image",
    "com.panasonic.raw-image",
    "com.panasonic.rw2-raw-image",
    "com.leica.raw-image",
    "com.leica.rwl-raw-image",
    "com.konicaminolta.raw-image",
    "com.olympus.sr-raw-image",
    "com.olympus.or-raw-image",
    "com.olympus.raw-image",
    "com.phaseone.raw-image",
    "public.jpeg-2000",
    "com.microsoft.ico",
    "com.microsoft.bmp",
    "com.adobe.photoshop-image",
    "com.microsoft.cur",
    "com.truevision.tga-image",
    "com.ilm.openexr-image",
    "public.radiance",
    "public.mpo-image",
    "public.pbm",
    "public.pvr",
    "com.apple.atx",
    "org.khronos.astc",
    "org.khronos.ktx",
    "com.microsoft.dds",
    "com.apple.rjpeg"
)
(
    "public.jpeg",
    "public.png",
    "com.compuserve.gif",
    "public.tiff",
    "public.jpeg-2000",
    "com.microsoft.ico",
    "com.microsoft.bmp",
    "com.adobe.photoshop-image",
    "com.adobe.pdf",
    "com.truevision.tga-image",
    "com.ilm.openexr-image",
    "public.pbm",
    "public.pvr",
    "com.apple.atx",
    "org.khronos.astc",
    "org.khronos.ktx",
    "com.microsoft.dds",
    "com.apple.rjpeg"
)

从上面可以看出,jpg,png,gif图片都能够支持

功能

  • 读取图片文件
使用CGImageSourceCreateWithURL读取原始图片数据。

    CGImageRef imageRef = NULL;
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"test100kBundle%ld",(long)index] ofType:@"png"];
    NSURL *url = [NSURL fileURLWithPath:path];
    CFStringRef keys[2];
    CFTypeRef values[2];
    keys[0] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
    keys[1] = kCGImageSourceShouldCache;
    values[0] = (CFTypeRef)kCFBooleanTrue;
    values[1] = (CFTypeRef)kCFBooleanFalse;
    CFDictionaryRef dictRef = CFDictionaryCreate(NULL, (const void **)keys, (const void **)values, 2, NULL, NULL);
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef) url, dictRef);
    CFRelease(dictRef);
    imageRef = CGImageSourceCreateImageAtIndex(source, 0, NULL);
    CFRelease(source);
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CFRelease(imageRef);
    return image;

读取Thumbnail缩略图

    CGImageRef imageRef = NULL;
    NSString *path = [[NSBundle mainBundle] pathForResource:[NSString stringWithFormat:@"test100kBundle%ld",(long)index] ofType:@"png"];
    NSURL *url = [NSURL fileURLWithPath:path];
    CFStringRef keys[2];
    CFTypeRef values[2];
    keys[0] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
    keys[1] = kCGImageSourceShouldCache;
    values[0] = (CFTypeRef)kCFBooleanTrue;
    values[1] = (CFTypeRef)kCFBooleanFalse;
    CFDictionaryRef dictRef = CFDictionaryCreate(NULL, (const void **)keys, (const void **)values, 2, NULL, NULL);
    CGImageSourceRef source = CGImageSourceCreateWithURL((CFURLRef) url, dictRef);
    CFRelease(dictRef);
    CFStringRef thumbKeys[3];
    CFTypeRef thumbValues[3];
    int maxSize = 128;
    thumbKeys[0] = kCGImageSourceCreateThumbnailWithTransform;
    thumbKeys[1] = kCGImageSourceCreateThumbnailFromImageIfAbsent;
    thumbKeys[2] = kCGImageSourceThumbnailMaxPixelSize;
    thumbValues[0] = kCFBooleanTrue;
    thumbValues[1] = kCFBooleanTrue;
    thumbValues[2] = CFNumberCreate(NULL, kCFNumberIntType, &maxSize);
    CFDictionaryRef imageDict = CFDictionaryCreate(NULL, (const void **)thumbKeys,(const void **)thumbValues, 3, NULL, NULL);
    imageRef = CGImageSourceCreateThumbnailAtIndex(source, 0, imageDict);
    CFRelease(source);
    CFRelease(imageDict);
    UIImage *image = [UIImage imageWithCGImage:imageRef];
    CFRelease(imageRef);
    return image;

上述两方法timer 分析:



从上面的结果可以看出,第一种方法并没有对图片进行解码操作,还是在设置了imageView之后系统解码了,第二种在创建缩略图的时候就进行了解码,耗时比第一种高一些,应该是提供的文件本省没有缩略图,所以需要解码文件之后再进行创建。

  • 写图片文件

使用CGImageDestinationRef进行写文件

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *docDir = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
    NSString *filePath = [docDir stringByAppendingPathComponent:@"test.gif"];
    NSURL *fileUrl = [NSURL fileURLWithPath:filePath];
    CGImageDestinationRef dest = CGImageDestinationCreateWithURL((CFURLRef)fileUrl, kUTTypeGIF, imageArray.count, NULL);
    NSMutableDictionary *dict = [NSMutableDictionary new];
    [dict setObject:@{
                      (__bridge NSString *)kCGImagePropertyGIFDelayTime:@((double)6.0 / imageArray.count),
                      } forKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];
    NSMutableDictionary *dictOne = [NSMutableDictionary new];
    [dictOne setObject:@{
                      (__bridge NSString *)kCGImagePropertyGIFDelayTime:@(1),
                      } forKey:(__bridge NSString *)kCGImagePropertyGIFDictionary];
    for (UIImage *image in imageArray)
    {
        if ([imageArray indexOfObject:image] < 3)
        {
            CGImageDestinationAddImage(dest, image.CGImage, (CFDictionaryRef)dictOne);
        }
        else
        {
            CGImageDestinationAddImage(dest, image.CGImage, (CFDictionaryRef)dict);
        }
    }
    if (CGImageDestinationFinalize(dest))
    {
        NSLog(@"gif 图片创建成功");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"gif 图片创建成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        [alertController addAction:defaultAction];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }
    else
    {
        NSLog(@"gif 图片创建失败");
        UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"提示" message:@"gif 图片创建失败" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                              handler:^(UIAlertAction * action) {
                                                                  
                                                              }];
        [alertController addAction:defaultAction];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }
    CFRelease(dest);

上面的代码是读取了30张图片,然后写到gif图片里面,并且在写的时候前三张的时间间隔设置了1s,其他都是0.2s。在mac上可以看到最后生成图片的效果。