我在看《 IOS7编程突破极限》一书中的以下代码,无法理解为什么作者不使用块就使用__block存储类型。我对__block的了解仅限于理解,即它们允许可变范围内的块捕获的变量是可变的。我还阅读了有关__block的其他SO帖子,但它们使我更加困惑。

-(IBAction)buttonAction:(id)sender {

  self.layer = [CALayer layer];
  self.layer.frame = CGRectMake(80, 100, 160, 160);
  [self.view.layer addSublayer:self.layer];

  float scale = [UIScreen mainScreen].scale;

  UIGraphicsBeginImageContextWithOptions(self.view.frame.size, YES, scale);
  [self.view drawViewHierarchyInRect:self.view.frame afterScreenUpdates:NO];

  __block UIImage *image = UIGraphicsGetImageFromCurrentImageContext();

  UIGraphicsEndImageContext();

  CGImageRef imageRef = CGImageCreateWithImageInRect(image.CGImage,
                                                     CGRectMake(self.layer.frame.origin.x * scale,
                                                                self.layer.frame.origin.y * scale,
                                                                self.layer.frame.size.width * scale,
                                                                self.layer.frame.size.height * scale));
  image = [UIImage imageWithCGImage:imageRef];
  image = [image applyBlurWithRadius:50.0f
                           tintColor:[UIColor colorWithRed:0 green:1 blue:0 alpha:0.1]
               saturationDeltaFactor:2.0f
                           maskImage:nil];

  self.layer.contents = (__bridge id)(image.CGImage);
}

最佳答案

我的猜测是编辑错误。该代码可能确实在某一时刻包含一个块,但是在以后的编辑中将其删除,但是作者忘记带走了__block限定符。

10-05 20:34