2

我有UIView它显示图表..现在我需要将它捕获到UIImage并用谷歌搜索它并得到下面的代码。但是如果我在我的代码中使用它它不起作用。即使我使用断点编译器也无法达到这一点。我在我的UIView.where 中使用这个我哪里出错了?

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

UIImage * img = UIGraphicsGetImageFromCurrentImageContext();

UIGraphicsEndImageContext();

return img;
} 
4

3 回答 3

3

您可以使用以下方法截取 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];

我希望这对你有帮助。

于 2012-12-27T05:34:28.270 回答
1

您可以像这样保存视图的图像并将图像存储到文档目录中:-

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

于 2012-12-27T05:37:42.073 回答
1

尝试使用此代码可能会对您有所帮助

- (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;
}
于 2012-12-27T05:56:05.963 回答