【问题标题】:How to capture a specific size of the self.view如何捕获特定大小的 self.view
【发布时间】:2012-10-02 14:46:41
【问题描述】:

我有 self.view,但我只想捕获它的 300x320。

得到这个代码:

UIGraphicsBeginImageContext(self.view.bounds.size);
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);

UIImage * imgPrintScreen = [[UIImage alloc]init];
imgPrintScreen = viewImage;

我需要在此处更改哪些内容?

感谢分配!

【问题讨论】:

    标签: objective-c ios uiimage capture


    【解决方案1】:

    只需更改您要捕获的大小,而不是使用整个边界,而是使用您想要的大小。渲染将从视图的原点开始,并在超出边界时被剪裁。如果您需要更改视图的实际剪辑位置,只需在启动图像上下文后翻译上下文即可:

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    例如,如果您的视图是 600x320 并且您想捕捉中间 300 点的宽度,您可以将上下文向左平移 150 点:

    UIGraphicsBeginImageContextWithOptions(CGSizeMake(300, 320), YES, 0.);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextTranslateCTM(context, -150.f, 0.f);
    [self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-24
      • 1970-01-01
      相关资源
      最近更新 更多