iOS開發 代碼 或 截屏


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 . 監聽手機<Home + Power>鍵截屏,並獲取截屏圖片

(1) . 在合適的位置給應用添加監聽,監聽手機截屏動作

/**
 *  為<Home + Power鍵>添加監聽
 *  selector 為監聽到截屏后調用的方法
 */
- (void) addScreenshotObserverWithSlelctor:(SEL)selector {
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(selector)
                                                 name:UIApplicationUserDidTakeScreenshotNotification object:nil];
    
}

(2) . 監聽到截屏在相冊中查找最新的一張圖片即為截屏圖片,添加 #import <Photos/Photos.h>

/**
 *  相冊中最新的一張圖片
 */
- (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 即為查找到的圖片,也是此時的截屏圖片
        }
    }];
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM