iOS 截屏功能代码

大家都知道通过同时按iphone和ipad关机键和home键可以进行截图,也可以通过代码编程进行对屏幕的截图。

使用opengles截图并保存代码:

相关连接

  1. -(UIImage *) glToUIImage  
  2.     NSInteger myDataLength 1024 768 4;  
  3.       
  4.     // allocate array and read pixels into it.  
  5.     GLubyte *buffer (GLubyte *) malloc(myDataLength);  
  6.     glReadPixels(0, 0, 1024, 768, GL_RGBA, GL_UNSIGNED_BYTE, buffer);  
  7.       
  8.     // gl renders "upside down" so swap top to bottom into new array.  
  9.     // there's gotta be better way, but this works.  
  10.     GLubyte *buffer2 (GLubyte *) malloc(myDataLength);  
  11.     for(int 0; <768; y++)  
  12.      
  13.         for(int 0; <1024 4; x++)  
  14.          
  15.             buffer2[(767 y) 1024 x] buffer[y 1024 x];  
  16.          
  17.      
  18.       
  19.     // make data provider with data.  
  20.     CGDataProviderRef provider CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);  
  21.       
  22.     // prep the ingredients  
  23.     int bitsPerComponent 8;  
  24.     int bitsPerPixel 32;  
  25.     int bytesPerRow 1024;  
  26.     CGColorSpaceRef colorSpaceRef CGColorSpaceCreateDeviceRGB();  
  27.     CGBitmapInfo bitmapInfo kCGBitmapByteOrderDefault;  
  28.     CGColorRenderingIntent renderingIntent kCGRenderingIntentDefault;  
  29.       
  30.     // make the cgimage  
  31.     CGImageRef imageRef CGImageCreate(1024, 768, bitsPerComponent, bitsPerPixel, bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO, renderingIntent);  
  32.       
  33.     // then make the uiimage from that  
  34.     UIImage *myImage [UIImage imageWithCGImage:imageRef];  
  35.     return myImage;  
  36.  
  37.   
  38. -(void)captureToPhotoAlbum  
  39.     UIImage *image [self glToUIImage];  
  40.     UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);  
  41.  
  42.  


使用苹果官网截图并保存

相关连接

  1. -(void)ScreenShots  
  2.  
  3.     [self unschedule:@selector(ScreenShots)];  
  4.       
  5.     CGSize imageSize [[UIScreen mainScreen] bounds].size;  
  6.     if (NULL != UIGraphicsBeginImageContextWithOptions)  
  7.         UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);  
  8.      
  9.     else  
  10.      
  11.         UIGraphicsBeginImageContext(imageSize);  
  12.      
  13.       
  14.     CGContextRef context UIGraphicsGetCurrentContext();  
  15.       
  16.     for (UIWindow window in [[UIApplication sharedApplication] windows])  
  17.         if (![window respondsToSelector:@selector(screen)] || [window screen] == [UIScreen mainScreen])  
  18.             CGContextSaveGState(context);  
  19.             CGContextTranslateCTM(context, [window center].x, [window center].y);  
  20.             CGContextConcatCTM(context, [window transform]);  
  21.             CGContextTranslateCTM(context, -[window bounds].size.width*[[window layer] anchorPoint].x, -[window bounds].size.height*[[window layer] anchorPoint].y);  
  22.             [[window layer] renderInContext:context];  
  23.               
  24.             CGContextRestoreGState(context);  
  25.          
  26.      
  27.       
  28.     UIImage *image UIGraphicsGetImageFromCurrentImageContext();  
  29.       
  30.     UIGraphicsEndImageContext();  
  31.     NSThread *thread=[[NSThread alloc] initWithTarget:self selector:@selector(saveThread:) object:image];  
  32.     [thread start];  
  33.     [thread release];  
  34.     
  35.     NSLog(@"Suceeded!");  
  36.  
  37.   
  38. -(void)saveThread:(UIImage*)image  
  39.  
  40.     NSAutoreleasePool *pool=[[NSAutoreleasePool alloc]init];  
  41.     UIImageWriteToSavedPhotosAlbum(image, self, nil, nil);  
  42.     [pool release];  
  43.  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值