ios 截屏方法

作者: selice | 来源:发表于2019-08-05 09:47 被阅读0次

全屏图:

-(void)fullScreenshots{
 
UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
 
    UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
 
    [screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
 
    UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
 
    UIGraphicsEndImageContext();
 
    UIImageWriteToSavedPhotosAlbum(viewImage, nil, nil, nil);
 
}

截取自定义的大小

iPhone开发应用中抓图程序案例实现是本文要介绍的内容,主要是通过代码来实现抓图程序,具体实现过程,一起来看详细代码。
 
    //获得屏幕图像  
    - (UIImage *)imageFromView: (UIView *) theView    
    {  
          
        UIGraphicsBeginImageContext(theView.frame.size);  
        CGContextRef context = UIGraphicsGetCurrentContext();  
        [theView.layer renderInContext:context];  
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
        UIGraphicsEndImageContext();  
          
        return theImage;  
    }  
     
    //获得某个范围内的屏幕图像  
    - (UIImage *)imageFromView: (UIView *) theView   atFrame:(CGRect)r  
    {  
        UIGraphicsBeginImageContext(theView.frame.size);  
        CGContextRef context = UIGraphicsGetCurrentContext();  
        CGContextSaveGState(context);  
        UIRectClip(r);  
        [theView.layer renderInContext:context];  
        UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();  
        UIGraphicsEndImageContext();  
          
        return  theImage;//[self getImageAreaFromImage:theImage atFrame:r];  
    }

//保存图片到本地

//保存图片到本地
- (void)saveImageToPhotos:(UIImage*)savedImage

{
    
    UIImageWriteToSavedPhotosAlbum(savedImage, self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
    
}
// 指定回调方法
- (void)image: (UIImage *) image didFinishSavingWithError: (NSError *) error contextInfo: (void *) contextInfo
{
    
    NSString *msg = nil ;
    
    if(error != NULL){
        
        msg = @"保存失败!" ;
        
    }else{
        
        msg = @"保存成功!" ;
        
    }
    
    [MBProgressHUD showError:msg toView:self.view];
}

关于直播间截屏 比较特殊。
目前多数的直播播放是使用openGLES来实现的,使用常用的

UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
   CGContextRef context = UIGraphicsGetCurrentContext();
   CGContextFillRect(context, bounds);
   CGContextTranslateCTM(context, shrinkSize.width, shrinkSize.height);
   [mainWindow.layer renderInContext:context];
   UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
   UIGraphicsEndImageContext();

不能截取播放器播放的画面,只能截取到一块黑屏
目前使用下面的方法可以直接截取

    UIWindow *mainWindow = [UIApplication sharedApplication].keyWindow;
    UIGraphicsBeginImageContextWithOptions(mainWindow.frame.size, NO, 0);
    [mainWindow drawViewHierarchyInRect:mainWindow.frame afterScreenUpdates:YES];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

相关文章

  • iOS屏幕截图功能

    iOS7.0之前的系统,可以通过以下代码实现截屏功能。 iOS7.0之后,系统中封装了截屏的方法- (UIView...

  • flutter:截屏

    1.flutter-截屏组件 2.flutter-截屏插件 3.flutter-iOS原生截屏 iOS代码 4.获...

  • (最新)iOS截屏

    ios webview 截屏:ios截屏 前言:介绍一下截屏有很多种做法1:截当前屏幕内容2:截整个视图的所有内容...

  • iOS截屏方法

    版权声明:出自MajorLMJ技术博客的原创作品 ,转载时必须注明出处及相应链接!

  • ios 截屏方法

    全屏图: 截取自定义的大小 //保存图片到本地 关于直播间截屏 比较特殊。目前多数的直播播放是使用openGLES...

  • ios截屏

    ios截屏

  • iOS 应用内截屏分享

    需求:捕获用户截屏操作,并建议用户截屏后的操作。虽然iOS11 有系统的截屏,但 APP 内截屏可便捷操作。 封装...

  • iOS 原生截屏方法

    -(UIImage*)captureScreenForView:(UIView*)currentView { ...

  • iOS 截屏&长截屏

    截屏在 iOS 开发中经常用到,本篇文章讲的是监听用户截屏操作,并且获取截屏图片,如果当前是UIScrollVie...

  • iOS开发中iOS10后系统截屏无法获取PDF内容

    PDF加载方法 使用WKWebView加载PDF(iOS10后系统截屏,无法获取到PDF内容) 使用PDFView...

网友评论

    本文标题:ios 截屏方法

    本文链接:https://www.haomeiwen.com/subject/nedpdctx.html