【发布时间】:2012-11-10 07:29:41
【问题描述】:
我是 iPhone 开发的新手。对 iPhone 技术中的视图和布局不太熟悉。
我要实现的是:
- 我在包含一个按钮的页面中,单击该按钮我打开图表视图(使用核心图)。
- 我想截取该图表视图的屏幕截图,但我不想打开该视图。
这可能吗?
我们将不胜感激。
【问题讨论】:
标签: iphone objective-c ios screenshot core-plot
我是 iPhone 开发的新手。对 iPhone 技术中的视图和布局不太熟悉。
我要实现的是:
这可能吗?
我们将不胜感激。
【问题讨论】:
标签: iphone objective-c ios screenshot core-plot
是的,您可以将视图层合成到位图上下文中,然后从中获取图像对象:
CGContextRef CGContextCreate(CGSize size)
{
CGColorSpaceRef space = CGColorSpaceCreateDeviceRGB();
CGContextRef ctx = CGBitmapContextCreate(NULL, size.width, size.height, 8, size.width * (CGColorSpaceGetNumberOfComponents(space) + 1), space, kCGImageAlphaPremultipliedLast);
CGColorSpaceRelease(space);
return ctx;
}
- (UIImage *)screenshotView:(UIView *)view
{
CGSize size = view.frame.size;
// Check for retina display
if ([[UIScreen mainScreen] scale] > 1.5f) {
size.width *= 2;
size.height *= 2;
}
CGContextRef ctx = CGContextCreate(size);
CGAffineTransform normalize = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
CGContextConcatCTM(ctx, normalize);
[[view layer] renderInContext:ctx];
CGImage cgImg = CGBitmapContextCreateImage(ctx);
UIImage *img = [UIImage imageWithCGImage:cgImg];
CGImageRelease(cgImg);
CGContextRelease(ctx);
return img;
}
【讨论】:
[viewController.view presentationLayer],也许? (请不要不可行!您应该能够修复崩溃...)
是的,你可以这样做。
UIView *yourView = /* get your chart view */;
UIGraphicsBeginImageContext(yourView.bounds.size);
[yourView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *yourViewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
现在你有了一个带有你的视图图像的 UIImage。
【讨论】:
UIGraphicsBeginImageContext(self.view.bounds.size); // You can put your view here.
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *data = [UIImagePNGRepresentation(image) retain];
UIImage *screenShot = [UIImage imageWithData:data];
【讨论】:
这是内置在核心情节中的:
UIImage *screenShot = [graph imageOfLayer];
graph 是您的核心绘图图 (CPTXYGraph),而不是托管视图。如果它不在屏幕某处已经可见的托管视图中,请务必在调用-imageOfLayer 之前设置图形的大小。如果您不打算在屏幕上显示它,则根本不需要托管视图。
【讨论】: