公司有个需求,做表情包功能,设计师会给我一套gif(包括做好的gif图和这个gif的每一张图片),结合视频录制功能,做成最长录制3秒的表情包,这个表情包的效果上有一些动态挂件,就像下图这种(图1):
不卖关子,我直接说我的逻辑:首先录制视频,录制完成后,根据录制时间,按每秒10张图片的规则来按帧取图片,将取出来的图片逐一和动态挂件的gif图的每张图片合成,最后用合成好的图片生成一张gif图。PS:这里我说一下,设计做的动态模板gif,每张图片的时间间隔是0.1S,这样做出来的效果比较好。并且合成gif前的每张图片的大小大概是35K左右,这里需要一些方法,我直接贴到下面,大家自行取用。
//图片合成 image1是背景图片 image2是gif图片
- (UIImage *)addImage:(UIImage *)image1 withImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
CGFloat image2Width =image2.size.width;
CGFloat image1Width =image1.size.width;
CGFloat image1Height = image1.size.height;
CGFloat image2Height = image2.size.height;
CGFloat resultImageHeight = 520;//我们拍的视频是520pt*520pt的(2倍图)
[image1 drawInRect:CGRectMake(0, 0, image1Width, image1Height) blendMode:kCGBlendModeNormal alpha:1];
[image2 drawInRect:CGRectMake(0,resultImageHeight-image2Height, image2Width*image1Width/resultImageHeight*1.0, image2Height*image1Width/resultImageHeight*1.0)];//因为软件生成的gif图片不是520*520的,所以保证合成的时候底边是对齐的
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *tData = UIImageJPEGRepresentation(resultingImage, 0.0);
UIImage *tImage = [UIImage imageWithData:tData];
UIImage *lastImage = [self imageWithImage:tImage scaledToSize:CGSizeMake(260, 260)];
UIGraphicsEndImageContext();
return lastImage;
}
1.图片合成的方法
//图片合成 image1是背景图片 image2是gif图片
- (UIImage *)addImage:(UIImage *)image1 withImage:(UIImage *)image2 {
UIGraphicsBeginImageContext(image1.size);
CGFloat image2Width =image2.size.width;
CGFloat image1Width =image1.size.width;
CGFloat image1Height = image1.size.height;
CGFloat image2Height = image2.size.height;
CGFloat resultImageHeight = 520;//我们拍的视频是520pt*520pt的(2倍图)
[image1 drawInRect:CGRectMake(0, 0, image1Width, image1Height) blendMode:kCGBlendModeNormal alpha:1];
[image2 drawInRect:CGRectMake(0,resultImageHeight-image2Height, image2Width*image1Width/resultImageHeight*1.0, image2Height*image1Width/resultImageHeight*1.0)];//因为软件生成的gif图片不是520*520的,所以保证合成的时候底边是对齐的
UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *tData = UIImageJPEGRepresentation(resultingImage, 0.0);
UIImage *tImage = [UIImage imageWithData:tData];
UIImage *lastImage = [self imageWithImage:tImage scaledToSize:CGSizeMake(260, 260)];
UIGraphicsEndImageContext();
return lastImage;
}
2./**
*根据给定的size的宽高比自动缩放原图片、自动判断截取位置,进行图片截取
* UIImage image 原始的图片
* CGSize size 截取图片的size
*/
-(UIImage *)clipImage:(UIImage *)image toRect:(CGSize)size{
//被切图片宽比例比高比例小 或者相等,以图片宽进行放大
if (image.size.width*size.height <= image.size.height*size.width) {
//以被剪裁图片的宽度为基准,得到剪切范围的大小
CGFloat width = image.size.width;
CGFloat height = image.size.width * size.height / size.width;
// 调用剪切方法
// 这里是以中心位置剪切,也可以通过改变rect的x、y值调整剪切位置
return [self imageFromImage:image inRect:CGRectMake(0, (image.size.height -height)/2, width, height)];
}else{ //被切图片宽比例比高比例大,以图片高进行剪裁
// 以被剪切图片的高度为基准,得到剪切范围的大小
CGFloat width = image.size.height * size.width / size.height;
CGFloat height = image.size.height;
// 调用剪切方法
// 这里是以中心位置剪切,也可以通过改变rect的x、y值调整剪切位置
return [self imageFromImage:image inRect:CGRectMake((image.size.width -width)/2, 0, width, height)];
}
return nil;
}
3.//获取某个帧的图片
- (UIImage *)frameImageFromVideoURL:(NSURL *)videoURL withPoint:(CMTime)point{
// result
UIImage *image = nil;
// AVAssetImageGenerator
AVAsset *asset = [AVAsset assetWithURL:videoURL];
AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
imageGenerator.appliesPreferredTrackTransform = YES;
imageGenerator.requestedTimeToleranceAfter = kCMTimeZero;//必须设置,否则时间对应不上
imageGenerator.requestedTimeToleranceBefore = kCMTimeZero;//必须设置,否则时间对应不上
// get the image from
NSError *error = nil;
CMTime actualTime;
// Returns a CFRetained CGImageRef for an asset at or near the specified time.
// So we should mannully release it
CGImageRef centerFrameImage = [imageGenerator copyCGImageAtTime:point
actualTime:&actualTime
error:&error];
if (centerFrameImage != NULL) {
image = [[UIImage alloc] initWithCGImage:centerFrameImage];
// Release the CFRetained image
CGImageRelease(centerFrameImage);
}
return image;
}
4.图片合成gif亲测可用,我测试的是真机版本的