在iphone中捕捉截图?

时间:2012-01-30 06:55:39

标签: iphone objective-c ios ipad

我想以编程方式截取屏幕截图。所以我为它编写代码,

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;

这里所有代码都在UIViewController的自定义UIView中。现在当我在UIView上播放UIImageView png序列动画时,我没有在UIImageView中获得更新,这是自定义UIView的子视图,为什么?我得到的结果只有第一个UIImage的UIImageView。

基本上我需要创建一个像汤姆应用程序一样的游戏视频。

3 个答案:

答案 0 :(得分:0)

以下代码用于将图像捕获到iPhone中以供以下代码使用。

 CGRect rect = CGRectMake(0.0, 0.0,self.view.frame.size.width,self.view.frame.size.height-44);
    NSValue *rectObj = [NSValue valueWithCGRect:rect];

    CGRect rectRestored = [rectObj CGRectValue];

    UIGraphicsBeginImageContext(CGSizeMake(rectRestored.size.width, rectRestored.size.height-44));
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

还将图像存储到图库中。

答案 1 :(得分:0)

你应该看看AVFoundation,特别是AVAssetWriter,作为制作屏幕内容视频的一种方式。

答案 2 :(得分:0)

使用以下内容获取ImageView的屏幕截图:

[[[yourImageView layer] presentationLayer] renderInContext:context];

使用NSTimer调用getScreenshot函数。

希望这有帮助

相关问题