Convert UIView Layer to UIImage

后端 未结 7 1408
深忆病人
深忆病人 2020-12-29 10:34

I\'m playing Video using AVPlayerLayer in a View. I need to convert View to Image, I tried

[myview.layer renderInContext:context];

but thi

相关标签:
7条回答
  • 2020-12-29 11:33

    This Code Useful when user want to capture current view ScreenShot and share or save this image....

    - (UIImage *)captureView {
    
    //hide controls if needed
        CGRect rect = [self.view bounds];
    
        UIGraphicsBeginImageContext(rect.size);
        CGContextRef context = UIGraphicsGetCurrentContext();
        [self.view.layer renderInContext:context];   
        UIImage *img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
    
    }
    

    See my this Answer Also.... My Answer

    UPDATE:

    AVPlayerItem *item = [AVPlayerItem playerItemWithURL:yourURL];
    AVPlayer *player = [AVPlayer playerWithPlayerItem:pItem];
    
    //observe 'status'
    [playerItem addObserver:self forKeyPath:@"status" options:0 context:nil];
    
    - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object
                            change:(NSDictionary *)change context:(void *)context
    { 
        if ([keyPath isEqualToString:@"status"]) {
            AVPlayerItem *item = (AVPlayerItem *)object;
            if (item.status == AVPlayerItemStatusReadyToPlay) {
                AVURLAsset *asset = (AVURLAsset *)item.asset;
                AVAssetImageGenerator *imageGenerator = [[AVAssetImageGenerator alloc] initWithAsset:asset];
                CGImageRef thumb = [imageGenerator copyCGImageAtTime:CMTimeMakeWithSeconds(10.0, 1.0)
                                                          actualTime:NULL
                                                               error:NULL];
            }
        }   
    }
    
    0 讨论(0)
提交回复
热议问题