Захват UIView и сохранение в виде изображения

Сначала я добавлю UILable в UIImageView, а затем после того, как снимок экрана UIView, изображение не правильно запечатлеет рамку UIView, также прикрепляю URL-адрес изображения.

enter image description here

1) Ссылка на исходное изображение: -

enter image description here

2) После захвата изображения Ссылка: -

Код: -

 UIGraphicsBeginImageContext(viewImage.frame.size);
    [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];

Ответы

Ответ 1

Вы можете сделать скриншоты любого UIView или захватить любой UIView и сохранить его как изображение с кодом ниже.

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

}

Ответ 2

Сделать снимок UIView

передать свое представление этой функции, он будет обрабатывать все сам (Не нужно устанавливать границы или фреймы)

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

Ответ 3

Версия Swift 3:

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
    }

Ответ 4

вы можете использовать приведенный ниже код для

UIGraphicsBeginImageContext(pictureView.bounds.size); 
                    

[pictureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

Ответ 5

Попробуйте это,

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];