在动画gif上添加标签和imageview并分享动画gif

时间:2014-01-28 05:48:56

标签: ios image animation gif caption

我正在制作图片标题应用。对于普通的png图片,一切正常,但我的客户要求我从图像中创建动画gif。我已经使用imageview动画属性创建了动画gif,但问题来自于我必须在gif图像上添加标题并分享带有标题的gif图像。我通过将它们转换为NSdata来共享简单的gif图像。请告诉我在gif动画上添加标题的正确方法。 我使用下面的代码来创建gif图像

 CGImageDestinationRef destination = CGImageDestinationCreateWithURL((CFURLRef)CFBridgingRetain([NSURL fileURLWithPath:path]),kUTTypeGIF,[camImages count],NULL);
 NSDictionary *frameProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:      [NSNumber numberWithInt:2] forKey:(NSString *)@"0.15f"]
 forKey:(NSString *)kCGImagePropertyGIFDictionary];
 NSDictionary *gifProperties = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount]
 forKey:(NSString *)kCGImagePropertyGIFDictionary];

_image=[[UIImage alloc] init];
for(int i=0;i<camImages.count;i++)
{
    _image =[camImages objectAtIndex:i];
    CGImageDestinationAddImage(destination, _image.CGImage, (CFDictionaryRef)CFBridgingRetain(frameProperties));
}
 CGImageDestinationSetProperties(destination, (CFDictionaryRef)CFBridgingRetain(gifProperties));
 CGImageDestinationFinalize(destination);

1 个答案:

答案 0 :(得分:2)

你拥有gif的每一帧,所以你可以在同一位置的gif的每个图像上绘制标题,然后你得到新的图像,你将把它们组成一个新的gif。

How to write text on image

这是一个旋转文本演示。

NSString * str = @"hello" ;
UIImage * image = [UIImage imageNamed:@"path"] ;
CGSize imageSize = image.size ;
UIGraphicsBeginImageContextWithOptions(imageSize, YES, 0.0) ;
[image drawInRect:CGRectMake(0, 0, imageSize.width, imageSize.height)] ;
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSaveGState(context);
CGContextRotateCTM(context, M_PI);
[[UIColor whiteColor] set] ;
[str drawInRect:CGRectMake(-imageSize.width, -imageSize.height, imageSize.width, imageSize.height) withFont:[UIFont systemFontOfSize:16.0]] ;
CGContextRestoreGState(context);
UIImage * compressedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();