图片合成GIF动画

153 阅读1分钟

#import <ImageIO/ImageIO.h>

#import <MobileCoreServices/MobileCoreServices.h>

+(void)creatGifWithImgArr:(NSMutableArray*)imgArr gifName:(NSString*)name folder:(NSString*)folder
{
    if (!imgArr||imgArr.count==0||name==nil||folder==nil) {return;}
    NSString* newPath = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"Documents/%@",folder]];
    NSString *path = [NSString stringWithFormat:@"%@/%@",newPath,name];
    CGImageDestinationRef destination =CGImageDestinationCreateWithURL((CFURLRef)[NSURL fileURLWithPath:path],kUTTypeGIF,imgArr.count,NULL);
    NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithFloat:0.2f] forKey:(NSString *)kCGImagePropertyGIFDelayTime] forKey:(NSString *)kCGImagePropertyGIFDictionary];
    NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:1] forKey:(NSString *)kCGImagePropertyGIFLoopCount]forKey:(NSString *)kCGImagePropertyGIFDictionary];
    @synchronized (imgArr) {
        for (NSString*imgN in imgArr) {
            UIImage *shacho = [UIImage imageNamed:imgN];
            CGImageDestinationAddImage(destination, shacho.CGImage, (CFDictionaryRef)frameProperties);
        }
    }
    CGImageDestinationSetProperties(destination,(CFDictionaryRef)gifProperties);
    CGImageDestinationFinalize(destination);
    CFRelease(destination);
}