【问题标题】:Take a snapshot of a PKAddPassesViewController's view拍摄 PKAddPassesViewController 视图的快照
【发布时间】:2013-06-06 13:04:57
【问题描述】:

我正在尝试对提示用户添加通行证时显示的视图进行快照。

目标是能够从给定的 pkpass 生成图像

我希望这样的事情可以奏效:

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"columbus" ofType:@"pkpass"];
PKPass *pass = [[PKPass alloc] initWithData:[NSData dataWithContentsOfFile:filePath] error:nil];

PKAddPassesViewController *pkAddPassesViewController = [[PKAddPassesViewController alloc] initWithPass:pass];
[self presentViewController:pkAddPassesViewController animated:YES completion:^(){
    UIImage *image = [pkAddPassesViewController.view captureView];
    UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
    [self.view addSubview:imageView];
}];

使用 captureView 是:

- (UIImage *)captureView
{
    CGRect screenRect = self.bounds;
    UIGraphicsBeginImageContext(self.bounds.size);
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    [[UIColor clearColor] set];
    CGContextFillRect(ctx, screenRect);
    [self.layer renderInContext:ctx];
    UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return newImage;
}

你猜怎么着?我错了:

我试过了:

  • 使用完成回调和animationFinished回调

  • 设置隐藏属性

  • 使用计时器确保在截取屏幕截图时可以在屏幕上看到通行证

  • 拍摄父视图的快照

我的想法已经用完了,所以任何建议都值得赞赏 提前致谢

哦,我在这里上传了一个最小的项目: https://github.com/framp/demo-capturing-passbook

编辑: 我可以拍摄其他视图的快照,并且可以使用上面的代码很好地加载其他 UIImage。 返回的 UIImage 是空白图片

【问题讨论】:

  • 这是我下周要与 Apple 讨论的事项之一。 PKPassViewController 似乎有些异常。如果我找到了,或者他们可以告诉我一个解决方案,我会在这里发布。
  • 我希望他们不是故意的(例如,阻止开发人员生成通行证图像)
  • 我也想过这个问题。我可以想到一些可能会带来安全风险的情况,但归根结底,我认为没有理由证明故意阻止是合理的。

标签: iphone ios objective-c passbook


【解决方案1】:

试试这个,这可能对你有用。

UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *newImage=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData=UIImagePNGRepresentation(reportImage);

【讨论】:

  • 谢谢,但这也不起作用。顺便说一句,我可以很好地拍摄其他视图的快照。
【解决方案2】:

捕获图像代码很好。尝试在完成块中使用:[self.view.superview addSubview:imageView]; 而不是 [self.view addSubview:imageView];

【讨论】:

  • 感谢您的建议!不幸的是,我从 captureView 收到的 UIImage 是空白图像(并且加载另一个 UIImage 有效)。
【解决方案3】:

如果对您有用,您可以尝试此代码。给出你想要捕捉的坐标,就是这样。它会将您的文件保存在相册中。它可能会发出警告,但它可以工作。

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

【讨论】:

  • 谢谢,但是 viewImage 仍然是空白的(这是意料之中的,因为代码是一样的)
  • 你怎么知道 viewImage 是空白的??
  • 如果您完全使用给定的代码,那么它应该将图像保存在相册中。检查它是否将图像保存在那里,请告诉我。
  • 我试过了,但它不适用于这个特定的 PKAddPassesViewController 视图。问题出在其他地方!
【解决方案4】:

我认为拍摄快照时pkAddPassesViewController 没有完全加载。所以我的建议是延迟一段时间后截取屏幕截图。 我希望它会起作用。

【讨论】:

  • 试过了。我在拍摄快照之前插入了一个断点,当我到达断点时,通行证显示出它的全部荣耀
  • 为什么不尝试在 ViewWillAppear 方法中的 pkAddPassesViewController 类中编写快照代码。
  • ViewDidAppear 没有被调用。我还注意到,如果应用程序在断点处等待,则传递视图控制器仍在工作并且响应良好
  • 现在是编写代码的时候了。请您与我分享您的代码,以便我也可以与您一起解决此问题。
  • 它在github上,我在问题的最后提到了url!
猜你喜欢
  • 2017-10-05
  • 1970-01-01
  • 2014-12-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-14
  • 1970-01-01
相关资源
最近更新 更多