豪捕捉到的UIView顶部的UIView(Howe to capture UIView top UI

2019-07-17 15:21发布

我有UIView其中显示Graph..now我需要把它捕捉到UIImage ,我用Google搜索,并得到以下code.but如果我使用这在我的代码,它不是work.even如果我使用断点编译器不会达到this.I我在我使用这个UIView 。凡我错了?

+ (UIImage *) imageWithView:(UIView *)view
{
UIGraphicsBeginImageContextWithOptions(view.bounds.size, view.opaque, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
} 

Answer 1:

你可以采取下面的方法iPhone应用程序的任何视图或整个屏幕截图

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

}

并调用它像波纹管...

UIImage *tempImageSave=[self captureView];

你也可以保存此图像与相册该波纹管行..

UIImageWriteToSavedPhotosAlbum(tempImageSave,nil,nil,nil);

你也可以保存此图像与文档目录此波纹管行..

NSData *imageData = UIImagePNGRepresentation(tempImageSave);
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];

如果视图包含层图像或一些图形有关的数据,然后使用以下方法。

-(UIImage *)convertViewToImage:(UIView *)viewTemp
{
    UIGraphicsBeginImageContext(viewTemp.bounds.size);
    [viewTemp drawViewHierarchyInRect:viewTemp.bounds afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return image;

}

而且使用这种方法如下面。

UIImage *tempImageSave = [self convertViewToImage:yourView];

我希望对您有所帮助。



Answer 2:

尝试使用此代码,可以帮助你

- (UIImage *)captureView:(UIView *)view {
    CGRect screenRect = [[UIScreen mainScreen] bounds];

    UIGraphicsBeginImageContext(screenRect.size);

    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(ctx, screenRect);

    [view.layer renderInContext:ctx];

    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

    return newImage;
}


Answer 3:

你可以保存照片喜欢这种方式和店面形象的图像中的文档目录: -

-(void)SaveViewAsImage
{
    UIGraphicsBeginImageContext(self.view.bounds.size);

        UIImage *saveImage = UIGraphicsGetImageFromCurrentImageContext();
        [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
        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];

}

您的图片在文档目录保存:)

下载演示

http://www.sendspace.com/file/gjxa82



文章来源: Howe to capture UIView top UIView