首页 > iOS中制作gif

iOS中制作gif

如题,如何在iOS中制作gif,就是使用本地的几张图片,做出一个gif,求思路,求第三方架构推荐,求求求求求!


(1). 思路的话,


// 创建 Gif 步骤。 基本上需要实现的就是这几个调用的methods  
String [] imageFilePaths = new String[]{"c:\\01.png","c:\\02.png","c:\\03.png"}; 
        String outputFilePath = "c:\\tmp\\test.gif";
        AnimatedGifEncoder encdr = new AnimatedGifEncoder();  //实例
        encdr.Start( outputFilePath );  
        encdr.SetDelay(500); // 图片间延迟
        encdr.SetRepeat(0);  
        for (int i = 0, count = imageFilePaths.Length; i < count; i++ ) 
        {
         encdr.AddFrame( Image.FromFile( imageFilePaths[i] ) ); // 添加图片, 每个图片叫做一帧
        }
        encdr.Finish();

// 读取 Gif 步骤和调用的methods 
        string outputPath = "c:\\tmp\\";
        GifDecoder gifDecoder = new GifDecoder();  //解码对象 实例
        gifDecoder.Read( "c:\\tmp\\test.gif" );
        for ( int i = 0, count = gifDecoder.GetFrameCount(); i < count; i++ ) 
        {
         Image frame = gifDecoder.GetFrame( i ); // 第i 帧的图片拿出来
         frame.Save( outputPath + Guid.NewGuid().ToString() 
                               + ".png", ImageFormat.Png );
        }

(2). 使用 NSData, 自己写文件, 进行类似于如下,


    memoryStream = new MemoryStream();
buf2 = new Byte[19];
buf3 = new Byte[8];
buf2[0] = 33;  //extension introducer
buf2[1] = 255; //application extension
buf2[2] = 11;  //size of block
buf2[3] = 78;  //N
buf2[4] = 69;  //E
buf2[5] = 84;  //T
buf2[6] = 83;  //S
buf2[7] = 67;  //C
buf2[8] = 65;  //A
buf2[9] = 80;  //P
buf2[10] = 69; //E
buf2[11] = 50; //2
buf2[12] = 46; //.
buf2[13] = 48; //0
buf2[14] = 3;  //Size of block
buf2[15] = 1;  //
buf2[16] = 0;  //
buf2[17] = 0;  //
buf2[18] = 0;  //Block terminator
buf3[0] = 33;  //Extension introducer
buf3[1] = 249; //Graphic control extension
buf3[2] = 4;   //Size of block
buf3[3] = 9;   //Flags: reserved, disposal method, user input, transparent color
buf3[4] = 10;  //Delay time low byte
buf3[5] = 3;   //Delay time high byte
buf3[6] = 255; //Transparent color index
buf3[7] = 0;   //Block terminator

(3). 使用Rob Mayoff建议的方法,


#import <UIKit/UIKit.h>
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
static UIImage *frameImage(CGSize size, CGFloat radians) {
    UIGraphicsBeginImageContextWithOptions(size, YES, 1); {
        [[UIColor whiteColor] setFill];
        UIRectFill(CGRectInfinite);
        CGContextRef gc = UIGraphicsGetCurrentContext();
        CGContextTranslateCTM(gc, size.width / 2, size.height / 2);
        CGContextRotateCTM(gc, radians);
        CGContextTranslateCTM(gc, size.width / 4, 0);
        [[UIColor redColor] setFill];
        CGFloat w = size.width / 10;
        CGContextFillEllipseInRect(gc, CGRectMake(-w / 2, -w / 2, w, w));
    }
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}
static void makeAnimatedGif(void) {
   static NSUInteger kFrameCount = 16;
   NSDictionary *fileProperties = @{
        (__bridge id)kCGImagePropertyGIFDictionary: @{
            (__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
        }
    };

   NSDictionary *frameProperties = @{
        (__bridge id)kCGImagePropertyGIFDictionary: @{
            (__bridge id)kCGImagePropertyGIFDelayTime: @0.02f, // a float (not double!) in seconds, rounded to centiseconds in the GIF data
        }
    };
   NSURL *documentsDirectoryURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:YES error:nil];
    NSURL *fileURL = [documentsDirectoryURL URLByAppendingPathComponent:@"animated.gif"];

CGImageDestinationRef destination = CGImageDestinationCreateWithURL((__bridge CFURLRef)fileURL, kUTTypeGIF, kFrameCount, NULL);
    CGImageDestinationSetProperties(destination, (__bridge CFDictionaryRef)fileProperties);

    for (NSUInteger i = 0; i < kFrameCount; i++) {
        @autoreleasepool {
            UIImage *image = frameImage(CGSizeMake(300, 300), M_PI * 2 * i / kFrameCount);
            CGImageDestinationAddImage(destination, image.CGImage, (__bridge CFDictionaryRef)frameProperties);
        }
    }

    if (!CGImageDestinationFinalize(destination)) {
        NSLog(@"failed to finalize image destination");
    }
    CFRelease(destination);

    NSLog(@"url=%@", fileURL);
}


http://www.v2ex.com/t/266387 这个还不错,不谢

【热门文章】
【热门文章】