iOS 怎么为GIF图片添加水印

先进行效果展示:

iOS 怎么为GIF图片添加水印_第1张图片

以下是主要代码部分:

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的路径

你可能感兴趣的:(iOS 怎么为GIF图片添加水印)