iOSで画面のスナップショットを撮る時のメモ

September 01, 2014

毎回検索したりしてたのでメモとして。ちょっと調べたら出てくるんだけど。

AppleのQ&Aが検索にヒットした。

- (UIImage *)snapshot:(UIView *)view { UIGraphicsBeginImageContextWithOptions(view.bounds.size, YES, 0); [view drawViewHierarchyInRect:view.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

return image;

}

とりあえず、これでViewのsnapshotは撮れる。僕はちょっと加工してUIViewのカテゴリとして使ったりしてる。

- (UIImage *)snapshot { UIGraphicsBeginImageContextWithOptions(self.bounds.size, YES, 0); [self drawViewHierarchyInRect:self.bounds afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext();

return image;

}

もっといい方法もありそうだけどとりあえず。iOS7から。iOS8全然調べてないけどもっと良いメソッド出てきそうだ。


Profile picture

Written by morizotter who lives and works in Tokyo building useful things. You should follow them on Twitter