Domanda

I am working on the picture caption app. For normal png picture all is working fine but my client ask me to create animated gif from the images. i have created animated gif using imageview animation property but the problem comes when i have to add caption on gif image and share that gif image with caption on it.I am sharing simple gif image by convering them into NSdata. Please advice me the right way to add caption on animated gif. I have used below code to create gif image

 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);
È stato utile?

Soluzione

You have every frame of the gif, so you can draw the title on every image of the gif on the same position, then you get new images and you will compose them into a new gif.

How to write text on image

It's a rotate text demo.

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();
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top