Simple version record of GIF in iOS

Source: Internet
Author: User

I have done an app about gif before, and I have never had time to record it. Now I want to record the learning process.

Creating a gif is based on ImageIO. framework. Therefore, you must add this library first.

 

Gif decomposed Image

 

 

-(Void) viewDidLoad {[super viewDidLoad]; NSData * data = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource: @ test101 ofType: @ gif]; // obtain the image data source CGImageSourceRef source = CGImageSourceCreateWithData (_ bridge CFDataRef) data, NULL) through data; // obtain the number of frames size_t count = CGImageSourceGetCount (source ); NSMutableArray * tmpArray = [NSMutableArray array]; for (size_t I = 0; I <count; I ++) {// obtain the image CGImageRef imageRef = CGImageSourceCreateImageAtIndex (source, I, NULL); // generate image UIImage * image = [UIImage imageWithCGImage: imageRef scale: [UIScreen mainScreen]. scale orientation: UIImageOrientationUp]; [tmpArray addObject: image]; CGImageRelease (imageRef);} CFRelease (source); int I = 0; for (UIImage * img in tmpArray) {// Write File NSData * imageData = UIImagePNGRepresentation (img); NSString * pathNum = [[self backPath] stringByAppendingPathComponent: [NSString stringWithFormat: @ % d.png, I]; [imageData writeToFile: pathNum atomically: NO]; I ++ }}// return the path to save the image-(NSString *) backPath {NSFileManager * fileManager = [NSFileManager defamanager]; NSString * path = [UniversalMethod backDocumentDirectoryPath]; NSString * imageDirectory = [path stringByAppendingPathComponent: @ Normal]; [fileManager createDirectoryAtPath: imageDirectory succeeded: YES attributes: nil error: nil]; return imageDirectory ;}

 

Gif Production

To create a gif, you must rely on MobileCoreServices. framework.

 

 

// Gif preparation // obtain the source data image NSMutableArray * imgs = [[NSMutableArray alloc] initWithObjects: [UIImage imageNamed: @ bear_1], [UIImage imageNamed: @ bear_2], nil]; // Image Target CGImageDestinationRef destination; // create the output path NSArray * document = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES); NSString * documentStr = [document objectAtIndex: 0]; NSFileManager * fileManager = [NSFileManager defaultManager]; NSString * textDirectory = [documentStr stringByAppendingPathComponent: @ gif]; [fileManager createDirectoryAtPath: textDirectory succeeded: YES attributes: nil error: nil]; NSString * path = [textDirectory stringByAppendingPathComponent: @test101.gif]; NSLog (@%@, path); // create a CFURL object/* struct (CFAllocatorRef allocator, CFStringRef filePath, CFURLPathStyle pathStyle, boolean isDirectory) allocator: distributor. Generally, kCFAllocatorDefault filePath: path pathStyle: path style is used. Fill in kCFURLPOSIXPathStyle. For more information, see "isDirectory" by yourself, used to specify whether filePath is resolved as a directory path */CFURLRef url = CFURLCreateWithFileSystemPath (kCFAllocatorDefault, (CFStringRef) path, kCFURLPOSIXPathStyle, false ); // return the image destination = CGImageDestinationCreateWithURL (url, kUTTypeGIF, imgs. count, NULL); // set gif information, playback interval, basic data, and delay time NSDictionary * frameProperties = [NSDictionary dictionaryWithObject: [NSMutableDictionary category: [NSNumber numberWithFloat: 0.3], (NSString *) delimiter, nil] forKey :( NSString *) delimiter]; // set the gif information NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithCapacity: 2]; [dict setObject: [NSNumber numberWithBool: YES] forKey :( NSString *) delimiter]; [dict setObject :( NSString *) delimiter forKey :( NSString *) kCGImagePropertyColorModel]; [dict setObject: [NSNumber numberWithInt: 8] forKey :( NSString *) kCGImagePropertyDepth]; [dict setObject: [NSNumber numberWithInt: 0] forKey :( NSString *) random]; NSDictionary * gifProperties = [NSDictionary dictionaryWithObject: dict forKey :( NSString *) kCGImagePropertyGIFDictionary]; // synthesize gif for (UIImage * dImg in imgs) {CGImageDestinationAddImage (destination, dImg. CGImage, (_ bridge CFDictionaryRef) frameProperties);} CGImageDestinationSetProperties (destination, (_ bridge CFDictionaryRef) gifProperties); CGImageDestinationFinalize (destination); CFRelease (destination );

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.