代码整理了2种截图,类似。(没苹果自带那种截图彻底)

方法一:

+(UIImage *)fullScreenshots{

    UIWindow *screenWindow = [[UIApplication sharedApplication] keyWindow];
// UIGraphicsBeginImageContext(screenWindow.frame.size);//全屏截图,包括window
UIGraphicsBeginImageContextWithOptions(screenWindow.frame.size,YES,0.0);
[screenWindow.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext(); return viewImage; }

  方法二:

+ (UIImage*)screenShot
{
// Create a graphics context with the target size
// On iOS 4 and later, use UIGraphicsBeginImageContextWithOptions to take the scale into consideration
// On iOS prior to 4, fall back to use UIGraphicsBeginImageContext
CGSize imageSize = [[UIScreen mainScreen] bounds].size;
UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
CGContextRef context = UIGraphicsGetCurrentContext(); // Iterate over every window from back to front
for (UIWindow *window in [[UIApplication sharedApplication] windows])
{
if ( [window screen] == [UIScreen mainScreen])
{
// -renderInContext: renders in the coordinate space of the layer,
// so we must first apply the layer's geometry to the graphics context
CGContextSaveGState(context);
// Center the context around the window's anchor point
CGContextTranslateCTM(context, [window center].x, [window center].y);
// Apply the window's transform about the anchor point
CGContextConcatCTM(context, [window transform]);
// Offset by the portion of the bounds left of and above the anchor point
CGContextTranslateCTM(context,
-[window bounds].size.width * [[window layer] anchorPoint].x,
-[window bounds].size.height * [[window layer] anchorPoint].y); // Render the layer hierarchy to the current context
[[window layer] renderInContext:context]; // Restore the context
CGContextRestoreGState(context);
}
} // Retrieve the screenshot image
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return image;
} 

openGL ES 截图

- (UIImage*) takePicture {
int s = 1;
UIScreen* screen = [UIScreen mainScreen];
if ([screen respondsToSelector:@selector(scale)]) {
s = (int) [screen scale];
} GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport); int width = viewport[2];
int height = viewport[3]; int myDataLength = width * height * 4;
GLubyte *buffer = (GLubyte *) malloc(myDataLength);
GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
glReadPixels(0, 0, width, height, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
for(int y1 = 0; y1 < height; y1++) {
for(int x1 = 0; x1 <width * 4; x1++) {
buffer2[(height - 1 - y1) * width * 4 + x1] = buffer[y1 * 4 * width + x1];
}
}
free(buffer); CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
int bitsPerComponent = 8;
int bitsPerPixel = 32;
int bytesPerRow = 4 * width;
CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
CGImageRef imageRef = CGImageCreate(width, height, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);
CGColorSpaceRelease(colorSpaceRef);
CGDataProviderRelease(provider);
UIImage *image = [ UIImage imageWithCGImage:imageRef scale:s orientation:UIImageOrientationUp ];
return image;
}

  

以及颜色判断:

+ (float)colorAtPixel:(CGPoint)point  image:(UIImage *)image
{
// Cancel if point is outside image coordinates
if (!CGRectContainsPoint(CGRectMake(0.0f, 0.0f, image.size.width, image.size.height), point)) {
return 1000.0;
}
NSInteger pointX = trunc(point.x);
NSInteger pointY = trunc(point.y);
CGImageRef cgImage = image.CGImage;
NSUInteger width = image.size.width;
NSUInteger height = image.size.height;
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
int bytesPerPixel = 4;
int bytesPerRow = bytesPerPixel * 1;
NSUInteger bitsPerComponent = 8;
unsigned char pixelData[4] = { 0, 0, 0, 0 };
CGContextRef context = CGBitmapContextCreate(pixelData,1,1,
bitsPerComponent,
bytesPerRow,
colorSpace,
kCGImageAlphaPremultipliedLast | kCGBitmapByteOrder32Big);
CGColorSpaceRelease(colorSpace);
CGContextSetBlendMode(context, kCGBlendModeCopy);
// Draw the pixel we are interested in onto the bitmap context
CGContextTranslateCTM(context, -pointX, pointY - (CGFloat)height);
CGContextDrawImage(context, CGRectMake(0.0f, 0.0f, (CGFloat)width, (CGFloat)height), cgImage);
CGContextRelease(context);
// Convert color values [0..255] to floats [0.0..1.0]
CGFloat red = (CGFloat)pixelData[0] / 1.0f;
CGFloat green = (CGFloat)pixelData[1] / 1.0;
CGFloat blue = (CGFloat)pixelData[2] / 1.0f;
// CGFloat alpha = (CGFloat)pixelData[3] / 255.0f;
// NSLog(@" colors: RGB %f %f %f %f", red, green, blue, alpha);
return red + green + blue;
}

  

最新文章

  1. Steve Loughran:Why not raid 0,its about time and snowflakes!!!
  2. .Net Log4Net配置多文件日志记录
  3. 关于使用READ TABLE语句的几点注意事项
  4. php中位运算的应用:货品的状态
  5. Python算法之---冒泡,选择,插入排序算法
  6. StateListDrawable 资源
  7. 玩转docker
  8. Django的安装配置和开发
  9. POJ 1002 487-3279 Trie解读
  10. SpringMVC 学习-异常处理 SimpleMappingExceptionResolver 类
  11. Godep的基本使用
  12. js的map遍历和array遍历
  13. tkinter——GUI设计实操
  14. Python脱产8期 Day05 2019/4/17
  15. CCF-再卖菜-20180904
  16. mvn package出现No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK
  17. JustOj 1974: 简单的事情 (组合数)
  18. vim 常用命令(一)特殊删除
  19. 两种常量类型-readonly和const
  20. python 安装 Scrapy 模块

热门文章

  1. 机器学习(Machine Learning)
  2. poj_3259 负权和环
  3. iOS UITextField更改placeholder颜色
  4. js原型链和继承
  5. Babel6.x的安装
  6. BSSID,SSID,ESSID区别
  7. php 验证上传图片尺寸
  8. 并发编程 - 进程 - 1.互斥锁/2.模拟抢票/3.互斥锁与join区别
  9. [golang]内存不断增长bytes.makeSlice
  10. windows中根据进程PID查找进程对象过程深入分析