【问题标题】:How to take clear screenShot IOS如何拍摄清晰的屏幕截图 IOS
【发布时间】:2017-12-23 02:45:41
【问题描述】:

我正在开发需要清晰屏幕截图的 IOS 应用程序,我已经尝试过

func captureView() -> UIImage {
        //hide controls if needed
        let rect: CGRect = self.imageView.frame
        UIGraphicsBeginImageContext(rect.size)
        let context: CGContextRef = UIGraphicsGetCurrentContext()!
        self.view.layer.renderInContext(context)
        let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return img
    }

但图像质量不好。请给我一些建议。

【问题讨论】:

标签: ios swift image screenshot


【解决方案1】:

换行

UIGraphicsBeginImageContext(rect.size)

UIGraphicsBeginImageContextWithOptions(rect.size, false, 0.0);

【讨论】:

  • 亲爱的你对相同的代码有什么想法吗..我没有从 imageView 得到正确的图像
  • 很好的解决方案,关键是将比例设置为0.0,截图将是全画质!
【解决方案2】:

在新文件中添加以下代码块。而且,你去吧。

extension UIView {

    // Convert a uiview to uiimage
        func captureView() -> UIImage {
            // Gradually increase the number for high resolution.     
            let scale = 1.0 

            UIGraphicsBeginImageContextWithOptions(bounds.size, opaque, scale)   

            layer.renderInContext(UIGraphicsGetCurrentContext()!)
            let image:UIImage  = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()

            return image
        }
    }

现在,您可以在 UIView 的任何子类和 UIView 本身上调用 captureView() 来获取视图的 UIImage。

您可以在UIGraphicsBeginImageContextWithOptions 方法中更改scale 的值以获得高分辨率图像。

【讨论】:

    【解决方案3】:

    您可以使用 IOSurface 私有框架来做到这一点

    IOMobileFramebufferConnection connect;
    kern_return_t result;
    IOSurfaceRef screenSurface = NULL;
    
    io_service_t framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleH1CLCD"));
    if(!framebufferService)
        framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleM2CLCD"));
    if(!framebufferService)
        framebufferService = IOServiceGetMatchingService(kIOMasterPortDefault, IOServiceMatching("AppleCLCD"));
    
    result = IOMobileFramebufferOpen(framebufferService, mach_task_self(), 0, &connect);
    
    result = IOMobileFramebufferGetLayerDefaultSurface(connect, 0, &screenSurface);
    
    uint32_t aseed;
    IOSurfaceLock(screenSurface, kIOSurfaceLockReadOnly, &aseed);
    uint32_t width = IOSurfaceGetWidth(screenSurface);
    uint32_t height = IOSurfaceGetHeight(screenSurface);
    
    CFMutableDictionaryRef dict;
    int pitch = width*4, size = width*height*4;
    int bPE=4;
    char pixelFormat[4] = {'A','R','G','B'};
    dict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
    CFDictionarySetValue(dict, kIOSurfaceIsGlobal, kCFBooleanTrue);
    CFDictionarySetValue(dict, kIOSurfaceBytesPerRow, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &pitch));
    CFDictionarySetValue(dict, kIOSurfaceBytesPerElement, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &bPE));
    CFDictionarySetValue(dict, kIOSurfaceWidth, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &width));
    CFDictionarySetValue(dict, kIOSurfaceHeight, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &height));
    CFDictionarySetValue(dict, kIOSurfacePixelFormat, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, pixelFormat));
    CFDictionarySetValue(dict, kIOSurfaceAllocSize, CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt32Type, &size));
    
    IOSurfaceRef destSurf = IOSurfaceCreate(dict);
    
    void* outAcc;
    IOSurfaceAcceleratorCreate(NULL, 0, &outAcc);
    
    IOSurfaceAcceleratorTransferSurface(outAcc, screenSurface, destSurf, dict, NULL);
    
    IOSurfaceUnlock(screenSurface, kIOSurfaceLockReadOnly, &aseed);
    CFRelease(outAcc);
    
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, IOSurfaceGetBaseAddress(destSurf), (width * height * 4), NULL);
    CGImageRef cgImage=CGImageCreate(width, height, 8,
                                     8*4, IOSurfaceGetBytesPerRow(destSurf),
                                     CGColorSpaceCreateDeviceRGB(), kCGImageAlphaNoneSkipFirst |kCGBitmapByteOrder32Little,
                                     provider, NULL,
                                     YES, kCGRenderingIntentDefault);
    UIImage *img = [UIImage imageWithCGImage:cgImage];
    

    【讨论】:

      【解决方案4】:

      使用

      UIGraphicsBeginImageContextWithOptions(your size, opaque, scale

      而不是

      UIGraphicsBeginImageContext(rect.size)

      【讨论】:

        【解决方案5】:

        UIGraphicsBeginImageContextWithOptions 中的最后一个参数是比例。如果您只需要高分辨率,请尝试将比例值设置为 1.0。

        func captureView() -> UIImage {
                //hide controls if needed
                let rect: CGRect = self.imageView.frame
        
                UIGraphicsBeginImageContextWithOptions(rect.size, true, 1.0)//add this line
        
                let context: CGContextRef = UIGraphicsGetCurrentContext()!
                self.view.layer.renderInContext(context)
                let img: UIImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
                return img
            }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-02-14
          • 2014-01-12
          • 1970-01-01
          • 1970-01-01
          • 2014-05-16
          • 1970-01-01
          • 2023-03-15
          • 1970-01-01
          相关资源
          最近更新 更多