2012-08-03 78 views
6

捕获iOS设备屏幕时,是否有方法捕获CAEmitterCells(使用CAEmitterLayer生成)?
UIGetScreenImage()工程,但因为它是一个私人的方法我不允许使用它。
UIGraphicsBeginImageContext似乎不起作用,粒子从结果图像中简单地省略。iOS:在屏幕上捕获CAEmitterLayer粒子

编辑: 这是我目前用来捕捉视图的代码。实际上,我使用由here提供的代码录制了30秒长的屏幕视频。它通过每秒记录25个图像本身(它的一个UIView子类)和它的子视图(在我们的例子中包括UIView的图层是CAEmitterLayer)并使用AVAssetWriter来组成记录。

这是相当口的,所以我只在这里放置相关的代码: 我使用XCode中的ARC工具ARC代码,所以代码可能有点不同于内存管理方式。

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

    return context; 
} 

//static int frameCount = 0;   //debugging 
- (void) drawRect:(CGRect)rect { 
    NSDate* start = [NSDate date]; 
    CGContextRef context = [self createBitmapContextOfSize:self.frame.size]; 

    //not sure why this is necessary...image renders upside-down and mirrored 
    CGAffineTransform flipVertical = CGAffineTransformMake(1, 0, 0, -1, 0, self.frame.size.height); 
    CGContextConcatCTM(context, flipVertical); 

    [self.layer renderInContext:context]; 

    CGImageRef cgImage = CGBitmapContextCreateImage(context); 
    UIImage* background = [UIImage imageWithCGImage: cgImage]; 
    CGImageRelease(cgImage); 

    self.currentScreen = background; 

    //debugging 
    //if (frameCount < 40) { 
    //  NSString* filename = [NSString stringWithFormat:@"Documents/frame_%d.png", frameCount]; 
    //  NSString* pngPath = [NSHomeDirectory() stringByAppendingPathComponent:filename]; 
    //  [UIImagePNGRepresentation(self.currentScreen) writeToFile: pngPath atomically: YES]; 
    //  frameCount++; 
    //} 

    //NOTE: to record a scrollview while it is scrolling you need to implement your UIScrollViewDelegate such that it calls 
    //  'setNeedsDisplay' on the ScreenCaptureView. 
    if (_recording) { 
     float millisElapsed = [[NSDate date] timeIntervalSinceDate:startedAt] * 1000.0; 
     [self writeVideoFrameAtTime:CMTimeMake((int)millisElapsed, 1000)]; 
    } 

    float processingSeconds = [[NSDate date] timeIntervalSinceDate:start]; 
    float delayRemaining = (1.0/self.frameRate) - processingSeconds; 

    CGContextRelease(context); 

    //redraw at the specified framerate 
    [self performSelector:@selector(setNeedsDisplay) withObject:nil afterDelay:delayRemaining > 0.0 ? delayRemaining : 0.01]; 
} 

真的希望这有助于。感谢您的支持!

+0

顺便说一句 - 你可以为'CGBitmapContextCreate()'的__data__参数传递NULL,在这种情况下,数据将被CG – nielsbot 2012-08-13 07:26:46

+0

自动分配/解除分配,我也没有看到你的渲染代码这个片段? – nielsbot 2012-08-13 07:27:39

+0

你能解决这个问题吗? – 2012-09-26 17:45:30

回答

0

您是否尝试过使用-[CALayer renderInContext:]

+0

我认为他想要整个屏幕图像。啊,也许你的意思是渲染他想要的视图(可能包含CAEmitterLayer)。听起来很有希望。 – 2012-08-08 18:07:01

+0

@DavidH正是...... – nielsbot 2012-08-08 19:47:46

+0

进一步思考它,'UIWindow'也是'UIView',因此有一个支持层 - 你可以渲染它。 – nielsbot 2012-08-08 19:50:30