【问题标题】:Capture some of the iphone screen截取部分iphone屏幕
【发布时间】:2012-03-27 12:50:04
【问题描述】:

我正在使用此代码来捕获我的屏幕。

        CGImageRef screen = UIGetScreenImage();
    UIImage* image = [UIImage imageWithCGImage:screen];
    CGImageRelease(screen);
    UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);

这段代码占据了整个屏幕。 我只需要其中一些...CGRect rect = CGRectMake (0,0,100,100);

我可以传递任何参数来获取它吗?

谢谢!

【问题讨论】:

    标签: iphone ios uiimage screen cgimage


    【解决方案1】:

    你还可以为你想要的矩形大小定义一个新的上下文:

    CGRect rect = CGRectMake (0,0,100,100);
    UIGraphicsBeginImageContext(rect);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *screenshot = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(screenshot, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
    

    注意:你需要在 import 中添加 QuartzCore:

    #import <QuartzCore/QuartzCore.h>
    

    您可以通过以下方式获得主视图:

    UIView *view = [[UIApplication sharedApplication].keyWindow rootViewController].view
    

    【讨论】:

    • 像这样获取最高级别​​的视图非常有帮助,谢谢!
    【解决方案2】:

    听起来您只是想要或需要裁剪 UIImage。

    查看此相关问题中的代码,看看它是否对您有帮助:

    How to crop the UIImage?

    【讨论】:

    • 无法获取特定尺寸的uiimage?
    【解决方案3】:

    使用此代码捕获屏幕并自动保存到iphone设备中的图像库。

     UIGraphicsBeginImageContext(self.view.frame.size);
    [self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-20
      • 2019-02-14
      • 2015-03-14
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多