我以前也有类似的问题。以下为您可能工作作为一种解决方法:
UIGraphicsBeginImageContextWithOptions(editingView.bounds.size, NO, 0.0);
[editingView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
注意,你可能会失去一些精度屏幕捕获,因为它可以排除模糊,以及一些核心动画功能。
编辑:
似乎能够同时与renderInContext
和drawViewHierarchyInRect
问题/权衡。下面的代码可能是值得一试(从here取供参考):
- (CGContextRef) createBitmapContextOfSize:(CGSize) size {
CGContextRef context = NULL;
CGColorSpaceRef colorSpace;
int bitmapByteCount;
int bitmapBytesPerRow;
bitmapBytesPerRow = (size.width * 4);
bitmapByteCount = (bitmapBytesPerRow * size.height);
colorSpace = CGColorSpaceCreateDeviceRGB();
if (bitmapData != NULL) {
free(bitmapData);
}
bitmapData = malloc(bitmapByteCount);
if (bitmapData == NULL) {
fprintf (stderr, "Memory not allocated!");
return NULL;
}
context = CGBitmapContextCreate (bitmapData,
size.width,
size.height,
8, // bits per component
bitmapBytesPerRow,
colorSpace,
kCGImageAlphaNoneSkipFirst);
CGContextSetAllowsAntialiasing(context,NO);
if (context== NULL) {
free (bitmapData);
fprintf (stderr, "Context not created!");
return NULL;
}
CGColorSpaceRelease(colorSpace);
CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, size.height);
CGContextConcatCTM(context, flipVertical);
return context;
}
然后,你可以这样做:
CGContextRef context = [self createBitmapContextOfSize:editingView.bounds.size];
[editingView.layer renderInContext:context];
CGImageRef cgImage = CGBitmapContextCreateImage(context);
UIImage* background = [UIImage imageWithCGImage: cgImage];
CGImageRelease(cgImage);
嗨阿兰·普尔,我尝试这样做。但是我无法获得CAEmitterLayer和UIView的快照。为此我找到了一些相关的核心动画,你可以参考这个链接快速的想法http://stackoverflow.com/questions/11926690/caemitterlayer-not-rendering-when-renderincontext-of-superlayer-is-called。并请帮助我这个。并请通过我提供的gif链接通过 –
这两个解决方案似乎存在问题/权衡。我已经用可能的选择更新了我的答案。代码来自一篇相当古老的文章,所以它可能会或可能不会工作。 –
不适用于me.missing CAEmitterLayer –