先進行效果展示:
以下是主要代碼部分:
1:找到GIF圖片的數組
-(NSArray *)imagesWithGif:(NSString *)gifName{
NSURL *fileUrl = [[NSBundle mainBundle] URLForResource:gifName withExtension:@"gif"];
CGImageSourceRef gifSource = CGImageSourceCreateWithURL((CFURLRef)fileUrl, NULL);
size_t gifCount = CGImageSourceGetCount(gifSource);
NSMutableArray *frames = [[NSMutableArray alloc]init];
for (size_t i = 0; i< gifCount; i++) {
CGImageRef imageRef = CGImageSourceCreateImageAtIndex(gifSource, i, NULL);
UIImage *image = [UIImage imageWithCGImage:imageRef];
UIImage *gifIma = [self imageWithLogoImage:image logo:[UIImage imageNamed:@"WechatIMG375.png"]];
[frames addObject:gifIma];
CGImageRelease(imageRef);
}
return frames;
}
2:為GIF的每張圖片添加水印
#pragma mark - 加圖片水印
-(UIImage *)imageWithLogoImage:(UIImage *)img logo:(UIImage *)logo
{
//get image width and height
int w = img.size.width;
int h = img.size.height;
int logoWidth = logo.size.width;
int logoHeight = logo.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
//create a graphic context with CGBitmapContextCreate
/*
data? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? 指向要渲染的繪制內存的地址。這個內存塊的大小至少是(bytesPerRow*height)個字節
width? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? bitmap的寬度,單位為像素
height? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? bitmap的高度,單位為像素
bitsPerComponent? ? ? ? 內存中像素的每個組件的位數.例如,對于32位像素格式和RGB 顏色空間,你應該將這個值設為8.
bytesPerRow? ? ? ? ? ? ? ? ? bitmap的每一行在內存所占的比特數
colorspace? ? ? ? ? ? ? ? ? ? ? bitmap上下文使用的顏色空間。
bitmapInfo? ? ? ? ? ? ? ? ? ? ? 指定bitmap是否包含alpha通道,像素中alpha通道的相對位置,像素組件是整形還是浮點型等信息的字符串。
*/
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
CGContextDrawImage(context, CGRectMake(0, 0, w, h), img.CGImage);
CGContextDrawImage(context, CGRectMake(w-logoWidth, 0, logoWidth, logoHeight), [logo CGImage]);
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
return [UIImage imageWithCGImage:imageMasked];
}
3:再次生成GIF圖片
-(NSString *)gifImage:(NSArray *)imageArrayM{
// 2. 創建Gif文件
NSArray * document = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentStr = [document objectAtIndex:0];
NSFileManager * fileManager = [NSFileManager defaultManager];
NSString * textDic = [documentStr stringByAppendingString:@"/gif"];
[fileManager createDirectoryAtPath:textDic withIntermediateDirectories:YES attributes:nil error:nil];
NSString * path = [textDic stringByAppendingString:@"test1.gif"];
NSLog(@"path: %@",path);
// 3. 配置gif屬性
CGImageDestinationRef destion;
// 將path映射成url對象
CFURLRef url = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, (CFStringRef)path, kCFURLPOSIXPathStyle, false);
destion = CGImageDestinationCreateWithURL(url, kUTTypeGIF, imageArrayM.count, NULL);
NSMutableDictionary * dictM = [NSMutableDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithFloat:0.3],(NSString *)kCGImagePropertyGIFDelayTime, nil];
NSDictionary * frameDic = [NSDictionary dictionaryWithObject:dictM forKey:(NSString *)kCGImagePropertyGIFDelayTime];
NSMutableDictionary * gifParaDict = [NSMutableDictionary dictionaryWithCapacity:2];
// 設置顏色
[gifParaDict setObject:[NSNumber numberWithBool:YES] forKey:(NSString *)kCGImagePropertyGIFHasGlobalColorMap];
// 設置模式
[gifParaDict setObject:(NSString *)kCGImagePropertyColorModelRGB forKey:(NSString *)kCGImagePropertyColorModel];
// 設置顏色深度
[gifParaDict setObject:[NSNumber numberWithInt:8] forKey:(NSString *)kCGImagePropertyDepth];
// 是否可以重復播放
[gifParaDict setObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
NSDictionary * gifProperty = [NSDictionary dictionaryWithObject:gifParaDict forKey:(NSString *)kCGImagePropertyGIFDictionary];
// 單幀添加到gif
for (UIImage * dImage in imageArrayM) {
CGImageDestinationAddImage(destion, dImage.CGImage, (__bridge CFDictionaryRef)frameDic);
}
CGImageDestinationSetProperties(destion, (__bridge CFDictionaryRef)gifProperty);
CGImageDestinationFinalize(destion);
CFRelease(url);
CFRelease(destion);
return path;
}
4:可以盡情的 去使用生成GIF圖片了 path進行新的GIF的路徑