传入一个imagesArray数组返回一个GIF(类型为UIImage)
+(UIImage*)animatedGifWithArray:(NSArray*)tabImage; 此类方法用来返回GIF的UIImage类型
CreatGif .h
//.h文件
#import <Foundation/Foundation.h>
@interface CreateGif : NSObject
+ (void) makeAnimatedGif:(NSArray *)tabImage :(UIViewController *)vc;
+(UIImage*)animatedGifWithArray:(NSArray*)tabImage;
@end
//CreatGif.m文件
#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
#import "CreateGif.h"
#import "UIImage+animatedGIF.h"
@implementation CreateGif
+ (void) makeAnimatedGif:(NSArray *)tabImage :(UIViewController *)vc {
NSUInteger kFrameCount = [tabImage count];
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @(0.3f), // 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 {
CGImageDestinationAddImage(destination, ((UIImage *)[tabImage objectAtIndex:i]).CGImage, (__bridge CFDictionaryRef)frameProperties);
}
}
if (!CGImageDestinationFinalize(destination)) {
NSLog(@"failed to finalize image destination");
}
CFRelease(destination);
NSLog(@"url=%@", fileURL);
return ;
NSData *data = [NSData dataWithContentsOfURL:fileURL];
NSLog(@"data length = %d", [data length]);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
UIImageWriteToSavedPhotosAlbum([UIImage animatedImageWithAnimatedGIFData:data], nil, nil, nil);
});
UIView *v = [[UIView alloc] initWithFrame:vc.view.frame];
UIImageView *imgageV = [[UIImageView alloc] initWithFrame:vc.view.frame];
imgageV.image = [UIImage animatedImageWithAnimatedGIFURL:fileURL];
[v addSubview:imgageV];
[vc.view addSubview:v];
}
+(UIImage *)animatedGifWithArray:(NSArray *)tabImage{
NSUInteger kFrameCount = [tabImage count];
NSDictionary *fileProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFLoopCount: @0, // 0 means loop forever
}
};
NSDictionary *frameProperties = @{
(__bridge id)kCGImagePropertyGIFDictionary: @{
(__bridge id)kCGImagePropertyGIFDelayTime: @(0.3f), // 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 {
CGImageDestinationAddImage(destination, ((UIImage *)[tabImage objectAtIndex:i]).CGImage, (__bridge CFDictionaryRef)frameProperties);
}
}
if (!CGImageDestinationFinalize(destination)) {
}
CFRelease(destination);
NSData *data = [NSData dataWithContentsOfURL:fileURL];
return [UIImage animatedImageWithAnimatedGIFData:data];
}
@end
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步