web-dev-qa-db-ja.com

UIViewをキャプチャして画像として保存

最初にUILableUIImageViewを追加し、次に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];
18
Rushabh

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;

}
25
Paras Joshi

IViewのスナップショットを撮る

この関数にビューを渡すと、すべてが処理されます(境界またはフレームを設定する必要はありません

- (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;
}
13
Xar E Ahmer

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
    }
9
Marckaraujo

以下のコードを使用して

UIGraphicsBeginImageContext(pictureView.bounds.size); 
                    

[pictureView.layer renderInContext:UIGraphicsGetCurrentContext()]; 

UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
2
Sonu

このようにしてみてください

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
Venk

キャプチャした画像が元の品質を維持したい場合は、これを試してください。

-(UIImage*)captureViewWithAllSubView:(UIView*)view{
   UIGraphicsBeginImageContextWithOptions(view.bounds.size, 
   view.opaque, 0.0f);
   [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES];
   UIImage *capturedImage = 
   UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();
   return capturedImage;}
0
Mehsam Saeed