Capture UIView and Save as Image

前端 未结 7 503
孤城傲影
孤城傲影 2020-12-02 19:31

First of i Add the UILable on UIImageView and then after i screenshot the UIView, the image not proper capture the UIView

相关标签:
7条回答
  • 2020-12-02 19:38

    Take snapshot of a UIView

    pass your view to this function it will handle everything itself( No need to set bounds or frames)

    - (UIImage *)takeSnapshotOfView:(UIView *)view
    {
        UIGraphicsBeginImageContext(CGSizeMake(view.frame.size.width, view.frame.size.height));
        [view drawViewHierarchyInRect:CGRectMake(0, 0, view.frame.size.width, view.frame.size.height) afterScreenUpdates:YES];
        UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        return image;
    }
    
    0 讨论(0)
  • 2020-12-02 19:41

    Swift 3 Version:

    func takeSnapshotOfView(view:UIView) -> UIImage? {
        UIGraphicsBeginImageContext(CGSize(width: view.frame.size.width, height: view.frame.size.height))
        view.drawHierarchy(in: CGRect(x: 0.0, y: 0.0, width: view.frame.size.width, height: view.frame.size.height), afterScreenUpdates: true)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image
    }
    
    0 讨论(0)
  • 2020-12-02 19:47

    you can use the below code to

    UIGraphicsBeginImageContext(pictureView.bounds.size); 
                    
    [pictureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    
    0 讨论(0)
  • 2020-12-02 19:48

    In Swift 5 as extension:

    extension UIView {
    
        func takeSnapshot() -> UIImage? {
            UIGraphicsBeginImageContext(CGSize(width: self.frame.size.width, height: self.frame.size.height - 5))
            let rect = CGRect(x: 0.0, y: 0.0, width: self.frame.size.width, height: self.frame.size.height)
            drawHierarchy(in: rect, afterScreenUpdates: true)
            let image = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return image
        }
    }
    
    extension UIImage {
    
        func saveToPhotoLibrary(_ completionTarget: Any?, _ completionSelector: Selector?) {
            DispatchQueue.global(qos: .userInitiated).async {
                UIImageWriteToSavedPhotosAlbum(self, completionTarget, completionSelector, nil)
            }
        }
    }
    
    extension UIAlertController {
    
        func present() {
            guard let controller = UIApplication.shared.windows.filter({$0.isKeyWindow}).first?.rootViewController else {
                return
            }
            controller.present(self, animated: true)
        }
    }
    

    In your UIView sub class:

    func saveImage() {
        let selector = #selector(self.onImageSaved(_:error:contextInfo:))
        takeSnapshot()?.saveToPhotoLibrary(self, selector)
    }
    
    @objc private func onImageSaved(_ image: UIImage, error: Error?, contextInfo: UnsafeRawPointer) {
        if let error = error {
            let ac = UIAlertController(title: "Save error", message: error.localizedDescription, preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            ac.present()
        } else {
            let ac = UIAlertController(title: "Saved!", message: "Your altered image has been saved to your photos.", preferredStyle: .alert)
            ac.addAction(UIAlertAction(title: "OK", style: .default))
            ac.present()
        }
        onImageSaved?()
    }
    
    0 讨论(0)
  • 2020-12-02 19:50

    Try like this,

    UIGraphicsBeginImageContext(viewImage.frame.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor blackColor] set];
    CGContextFillRect(ctx, viewImage.frame);     
    
    [viewImage.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *vwImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    NSData *data=UIImagePNGRepresentation(vwImage);
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    // NSString *imgName = [NSString stringWithFormat:imagename];
    NSString *strPath = [documentsDirectory stringByAppendingPathComponent:imagename];
    [data writeToFile:strPath atomically:YES];
    
    0 讨论(0)
  • 2020-12-02 19:53

    You can take the screenshots of any UIView or capture any UIView and save it as an image with below code.

    - (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;
    
    }
    
    0 讨论(0)
提交回复
热议问题