我有一些视频和图像的收藏视图
使用 AVFoundation 能够从 iPhone 捕获视频并使用 AVAssetImageGenerator 生成缩略图。在画廊图像中显示时,应区分它的视频缩略图。所以我需要通过在其上绘制视频符号(如播放图标)来转换精确的图像。
可能吗?
我有一些视频和图像的收藏视图
使用 AVFoundation 能够从 iPhone 捕获视频并使用 AVAssetImageGenerator 生成缩略图。在画廊图像中显示时,应区分它的视频缩略图。所以我需要通过在其上绘制视频符号(如播放图标)来转换精确的图像。
可能吗?
您可以使用CoreGraphics
来编辑图像。
UIImage
首先,使用您要编辑的图像创建一个。然后,做这样的事情:
UIImage *oldThumbnail; //set this to the original thumbnail image
UIGraphicsBeginImageContext(oldThumbnail.size);
[oldThumbnail drawInRect:CGRectMake(0, 0, oldThumbnail.size.width, oldThumbnail.size.height)];
/*Now there are two ways to draw the play symbol.
One would be to have a pre-rendered play symbol that you load into a UIImage and draw with drawInRect */
UIImage *playSymbol = [UIImage imageNamed:"PlaySymbol.png"];
CGRect playSymbolRect; //I'll let you figure out calculating where you should draw the play symbol
[playSymbol drawInRect: playSymbolRect];
//The other way would be to draw the play symbol directly using CoreGraphics calls. Start with this:
CGContextRef context = UIGraphicsGetCurrentContext();
//now use CoreGraphics calls. I won't go over it here, but
the second answer to this question
may be helpful.
//Once you have finished drawing your image, you can put it in a UIImage.
UIImage *newThumbnail = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); //make sure you remember to do this :)
现在你可以在 UIImageView 中使用新生成的缩略图,缓存它,这样你就不需要每次都重新渲染它,等等。
这应该有效(您可能必须使用位置和尺寸):
-(UIImage*)drawPlayButton:(UIImage*)image
{
UIImage *playButton = [UIImage imageNamed:@"playbutton.png"];
UIGraphicsBeginImageContext(image.size);
[image drawInRect:CGRectMake(0, 0, image.size.width, image.size.height)];
[playButton drawInRect:CGRectMake(image.size.width/2-playButton.size.width/2, image.size.height/2-playButton.size.height/2, playButton.size.width, playButton.size.height)];
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return result;
}