Convert UIView Layer to UIImage

后端 未结 7 1407
深忆病人
深忆病人 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:11

    The MPMoviePlayerController makes it easy to get a image from a movie at a certain point in the movie.

        - (UIImage*)imageFromVideoAtPath:(NSString *)path atTime:(NSTimeInterval)time {
        NSURL *videoURL = [NSURL fileURLWithPath:path];
        MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];
        [moviePlayer prepareToPlay];
        UIImage *thumbnail = [moviePlayer thumbnailImageAtTime:time timeOption:MPMovieTimeOptionNearestKeyFrame];
        [moviePlayer stop];
        return thumbnail;
    }
    

    Just call this with the path of the video, and at the time you want to get the image.

    0 讨论(0)
  • 2020-12-29 11:14

    Try taking a screenshot and clipping. Otherwise you may have to render view and all its subviews recursively.

    -(UIImage *)videoScreenCap:(CGRect)cropRect{
    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
        UIGraphicsBeginImageContextWithOptions(self.window.bounds.size, NO, [UIScreen mainScreen].scale);
    else
        UIGraphicsBeginImageContext(self.window.bounds.size);
        [self.window.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        NSData * data = UIImagePNGRepresentation(image);
        [data writeToFile:@"foo.png" atomically:YES];
    CGImageRef imageRef = CGImageCreateWithImageInRect([largeImage CGImage], cropRect);
    UIImage * img = [UIImage imageWithCGImage:imageRef]; 
    CGImageRelease(imageRef);
    return img;
    }
    
    0 讨论(0)
  • 2020-12-29 11:17

    you can save your UIView as Image in Document Diretory like:

    -(IBAction)ViewTOimage:(id)sender
    {
    
    
        UIGraphicsBeginImageContext(self.view.bounds.size); //instad of self.view you can set your view IBOutlet name 
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
        NSData *imageData = UIImagePNGRepresentation(saveImage);
        NSFileManager *fileMan = [NSFileManager defaultManager];
    
        NSString *fileName = [NSString stringWithFormat:@"%d.png",1];
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *pdfFileName = [documentsDirectory stringByAppendingPathComponent:fileName];
        [fileMan createFileAtPath:pdfFileName contents:imageData attributes:nil];
    
    
    
    }
    
    0 讨论(0)
  • 2020-12-29 11:21
    -(UIImage *)imagefromview {
        UIGraphicsBeginImageContext(view.frame.size);
        [[self imageFromView:view] drawInRect:view.frame];
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return resultingImage;
    }
    
    0 讨论(0)
  • 2020-12-29 11:27

    I one of the project i used the below code to take an image from playing video might this help you too... take a look and try adjusting the code with your's.

    #import <AVFoundation/AVFoundation.h>
    
    @property (nonatomic,retain) AVCaptureStillImageOutput * resultImageOutput;
    
    - (UIImage*)stillImageFromVideo
    {  
        AVCaptureConnection *videoConnection = nil;
        for (AVCaptureConnection *connection in [[self resultImageOutput] connections]) {
            for (AVCaptureInputPort *port in [connection inputPorts]) {
                if ([[port mediaType] isEqual:AVMediaTypeVideo]) {
                    videoConnection = connection;
                    break;
                }
            }
            if (videoConnection) { 
                break; 
            }
        }
    
        [[self resultImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection 
        completionHandler:^(CMSampleBufferRef imageSampleBuffer, NSError *error) { 
             CFDictionaryRef exifAttachments = CMGetAttachment(imageSampleBuffer,   
       kCGImagePropertyExifDictionary, NULL);
    
            NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageSampleBuffer];    
            UIImage *image = [[UIImage alloc] initWithData:imageData];
            return image;  
        }
    }
    
    0 讨论(0)
  • 2020-12-29 11:31

    Please try below code. It is working perfectly for me.

    And refer UIKit Function Reference.

    + (UIImage *) imageWithView:(UIView *)view
    {
        UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
        [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
    
        UIGraphicsEndImageContext();
    
        return img;
    }
    

    Also try this.

    0 讨论(0)
提交回复
热议问题