在目标c中渲染现有图像上的播放图像

时间:2016-04-02 09:54:11

标签: objective-c iphone avfoundation avassetimagegenerator

我有一些视频和图像的收藏视图

使用AVFoundation能够从iPhone捕获视频并使用AVAssetImageGenerator生成缩略图。当在图库中显示时,应该区分它的视频缩略图。因此,我需要通过在其上绘制视频符号(如播放图标)来转换精确图像。

有可能吗?

2 个答案:

答案 0 :(得分:0)

您可以使用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中使用新生成的缩略图,对其进行缓存,这样您就不需要每次都重新渲染它等等。

答案 1 :(得分:0)

这应该有效(你可能需要玩位置和尺寸):

-(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;
}