iOS开发 代码 或 截屏

时间:2023-03-09 00:26:09
iOS开发 代码 或 <Home+Power>截屏

1. 截屏的两种简单方法,

注意这两种截图方法,都必须在视图完全加载完成后才能截图,即在 viewDidAppear 方法之后截屏,否则无法得到想要的截屏效果

(1) 利用绘图方法 renderInContext

/**
* 截取当前屏幕的内容
*/
- (void)snapshotScreen
{
// 判断是否为retina屏, 即retina屏绘图时有放大因子
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
UIGraphicsBeginImageContextWithOptions(self.view.window.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(self.view.window.bounds.size);
}
[self.view.window.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
// 保存到相册
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

该方法可以更改为截取某个视图的内容

/**
* 截取某视图的内容
*/
- (void)snapshotScreenInView:(UIView *)view
{
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]){
UIGraphicsBeginImageContextWithOptions(view.bounds.size, NO, [UIScreen mainScreen].scale);
} else {
UIGraphicsBeginImageContext(view.bounds.size);
}
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil); }

(2) . 利用iOS 7之后UIView提供的方法 drawViewHierarchyInRect: afterScreenUpdates:

/**
* 截取一个contentView的内容
*
* @param contentView
*/
- (void)snapshotScreenInView:(UIView *)contentView {
CGSize size = contentView.bounds.size;
UIGraphicsBeginImageContextWithOptions(size, NO, [UIScreen mainScreen].scale);
CGRect rect = contentView.frame;
// 自iOS7开始,UIView类提供了一个方法-drawViewHierarchyInRect:afterScreenUpdates: 它允许你截取一个UIView或者其子类中的内容,并且以位图的形式(bitmap)保存到UIImage中
[contentView drawViewHierarchyInRect:rect afterScreenUpdates:YES]; UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}

当然以上两种方法,我还没办法截屏到Status Bar(时间,网络状态等),如果截图中需要这些信息,就可以通过<home+power键>来截屏获取截屏图片

2 . 监听手机键截屏,并获取截屏图片

(1) . 在合适的位置给应用添加监听,监听手机截屏动作

/**
* 为<Home + Power键>添加监听
* selector 为监听到截屏后调用的方法
*/
- (void) addScreenshotObserverWithSlelctor:(SEL)selector { [[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(selector)
name:UIApplicationUserDidTakeScreenshotNotification object:nil]; }

(2) . 监听到截屏在相册中查找最新的一张图片即为截屏图片,添加 #import

/**
* 相册中最新的一张图片
*/
- (void)latestAssetImage { // 获取所有资源的集合,并按资源的创建时间排序
PHFetchOptions *options = [[PHFetchOptions alloc] init];
options.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"creationDate" ascending:NO]];
PHFetchResult *assetsFetchResults = [PHAsset fetchAssetsWithOptions:options];
PHAsset *asset = [assetsFetchResults firstObject];
// 使用PHImageManager从PHAsset中请求图片
PHImageManager *imageManager = [[PHImageManager alloc] init];
[imageManager requestImageForAsset:asset targetSize:PHImageManagerMaximumSize contentMode:PHImageContentModeAspectFill options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info) {
if (result) {
// result 即为查找到的图片,也是此时的截屏图片
}
}];
}