几种截屏方法

举报
清雨小竹 发表于 2022/09/25 02:31:46 2022/09/25
【摘要】 1.UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomScale); [pageView.page.layer renderInContext:UIGraphicsGetCurrentContext()]; U...

  
  1. 1.
  2. UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomScale);
  3. [pageView.page.layer renderInContext:UIGraphicsGetCurrentContext()];
  4. UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();
  5. UIGraphicsEndImageContext();
  6. 2.
  7. - (UIImage *) glToUIImage {
  8. DWScrollView *pageView = [self getActivePageView];
  9. pageView.page.backgroundColor = [UIColor clearColor];
  10. // self.backgroundColor=[UIColor clearColor];
  11. NSInteger myDataLength = 320 * 308 * 4;
  12. // allocate array and read pixels into it.
  13. GLubyte *buffer = (GLubyte *) malloc(myDataLength);
  14. glReadPixels(0, 0, 320, 308, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
  15. // gl renders "upside down" so swap top to bottom into new array.
  16. // there's gotta be a better way, but this works.
  17. GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
  18. for(int y = 0; y <308; y++)
  19. {
  20. for(int x = 0; x <320 * 4; x++)
  21. {
  22. if(buffer[y* 4 * 320 + x]==0)
  23. buffer2[(307 - y) * 320 * 4 + x]=1;
  24. else
  25. buffer2[(307 - y) * 320 * 4 + x] = buffer[y* 4 * 320 + x];
  26. }
  27. }
  28. // make data provider with data.
  29. CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
  30. // prep the ingredients
  31. int bitsPerComponent = 8;
  32. int bitsPerPixel = 32;
  33. int bytesPerRow = 4 * 320;
  34. CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
  35. CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
  36. CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
  37. // make the cgimage
  38. CGImageRef imageRef = CGImageCreate(320, 308, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
  39. // then make the uiimage from that
  40. UIImage *myImage = [UIImage imageWithCGImage:imageRef];
  41. UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, nil);
  42. return myImage;
  43. }
  44. 3.
  45. // get screen
  46. - (void)grabScreen {
  47. unsigned char buffer[320*480*4];
  48. glReadPixels(0,0,320,480,GL_RGBA,GL_UNSIGNED_BYTE,&buffer);
  49. CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, 320*480*4, NULL);
  50. CGImageRef iref = CGImageCreate(320,480,8,32,320*4,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault,ref,NULL,true,kCGRenderingIntentDefault);
  51. CGFloat width = CGImageGetWidth(iref);
  52. CGFloat height = CGImageGetHeight(iref);
  53. size_t length = width*height*4;
  54. uint32_t *pixels = (uint32_t *)malloc(length);
  55. CGContextRef context = CGBitmapContextCreate(pixels, width, height, 8, 320*4, CGImageGetColorSpace(iref), kCGImageAlphaLast | kCGBitmapByteOrder32Big);
  56. CGContextTranslateCTM(context, 0.0, height);
  57. CGContextScaleCTM(context, 1.0, -1.0);
  58. CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), iref);
  59. CGImageRef outputRef = CGBitmapContextCreateImage(context);
  60. UIImage *outputImage = [UIImage imageWithCGImage:outputRef];
  61. UIImageWriteToSavedPhotosAlbum(outputImage, nil, nil, nil);
  62. CGContextRelease(context);
  63. CGImageRelease(iref);
  64. CGDataProviderRelease(ref);
  65. }
  66. 4.
  67. CGImageRef UIGetScreenImage();
  68. void SaveScreenImage(NSString *path)
  69. {
  70. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  71. CGImageRef cgImage = UIGetScreenImage();
  72. void *imageBytes = NULL;
  73. if (cgImage == NULL) {
  74. CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
  75. imageBytes = malloc(320 * 480 * 4);
  76. CGContextRef context = CGBitmapContextCreate(imageBytes, 320, 480, 8, 320 * 4, colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
  77. CGColorSpaceRelease(colorspace);
  78. for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
  79. CGRect bounds = [window bounds];
  80. CALayer *layer = [window layer];
  81. CGContextSaveGState(context);
  82. if ([layer contentsAreFlipped]) {
  83. CGContextTranslateCTM(context, 0.0f, bounds.size.height);
  84. CGContextScaleCTM(context, 1.0f, -1.0f);
  85. }
  86. [layer renderInContext:(CGContextRef)context];
  87. CGContextRestoreGState(context);
  88. }
  89. cgImage = CGBitmapContextCreateImage(context);
  90. CGContextRelease(context);
  91. }
  92. NSData *pngData = UIImagePNGRepresentation([UIImage imageWithCGImage:cgImage]);
  93. CGImageRelease(cgImage);
  94. if (imageBytes)
  95. free(imageBytes);
  96. [pngData writeToFile:path atomically:YES];
  97. [pool release];
  98. }
  99. 5.
  100. + (UIImage *)imageWithScreenContents
  101. {
  102. CGImageRef cgScreen = UIGetScreenImage();
  103. if (cgScreen) {
  104. UIImage *result = [UIImage imageWithCGImage:cgScreen];
  105. CGImageRelease(cgScreen);
  106. return result;
  107. }
  108. return nil;
  109. }
  110. 在程序中如何把两张图片合成为一张图片
  111. - (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {
  112. UIGraphicsBeginImageContext(image1.size);
  113. // Draw image1
  114. [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];
  115. // Draw image2
  116. [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];
  117. UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();
  118. UIGraphicsEndImageContext();
  119. return resultingImage;
  120. }


文章来源: zzzili.blog.csdn.net,作者:清雨小竹,版权归原作者所有,如需转载,请联系作者。

原文链接:zzzili.blog.csdn.net/article/details/9097135

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。