搜索
简帛阁>技术文章>IOS开发-几种截屏方法

IOS开发-几种截屏方法

IOS开发-几种截屏方法

 

1.
        UIGraphicsBeginImageContextWithOptions(pageView.page.bounds.size, YES, zoomScale);
        [pageView.page.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage *uiImage = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
2.
- (UIImage *) glToUIImage {
       DWScrollView *pageView = [self getActivePageView];
       pageView.page.backgroundColor = [UIColor clearColor];
       // self.backgroundColor=[UIColor clearColor];
       NSInteger myDataLength = 320 * 308 * 4;
    
        // allocate array and read pixels into it.
        GLubyte *buffer = (GLubyte *) malloc(myDataLength);
        glReadPixels(0, 0, 320, 308, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
    
        // gl renders "upside down" so swap top to bottom into new array.
        // there's gotta be a better way, but this works.
        GLubyte *buffer2 = (GLubyte *) malloc(myDataLength);
    
        for(int y = 0; y <308; y++)
        {
            for(int x = 0; x <320 * 4; x++)
            {
                if(buffer[y* 4 * 320 + x]==0)
                    buffer2[(307 - y) * 320 * 4 + x]=1;
                else
                    buffer2[(307 - y) * 320 * 4 + x] = buffer[y* 4 * 320 + x];
            }
        }
    
    // make data provider with data.
    CGDataProviderRef provider = CGDataProviderCreateWithData(NULL, buffer2, myDataLength, NULL);
    
    // prep the ingredients
    int bitsPerComponent = 8;
    int bitsPerPixel = 32;
    int bytesPerRow = 4 * 320;
    CGColorSpaceRef colorSpaceRef = CGColorSpaceCreateDeviceRGB();
    CGBitmapInfo bitmapInfo = kCGBitmapByteOrderDefault;
    CGColorRenderingIntent renderingIntent = kCGRenderingIntentDefault;
    
    // make the cgimage
    CGImageRef
imageRef = CGImageCreate(320, 308, bitsPerComponent, bitsPerPixel,
bytesPerRow, colorSpaceRef, bitmapInfo, provider, NULL, NO,
renderingIntent);
    
    // then make the uiimage from that
    UIImage *myImage = [UIImage imageWithCGImage:imageRef];
    UIImageWriteToSavedPhotosAlbum(myImage, nil, nil, nil);
    return myImage;
}

3.
// get screen
- (void)grabScreen {
    unsigned char buffer[320*480*4];
    glReadPixels(0,0,320,480,GL_RGBA,GL_UNSIGNED_BYTE,&buffer);
    
    CGDataProviderRef ref = CGDataProviderCreateWithData(NULL, &buffer, 320*480*4, NULL);
    CGImageRef
iref =
CGImageCreate(320,480,8,32,320*4,CGColorSpaceCreateDeviceRGB(),kCGBitmapByteOrderDefault,ref,NULL,true,kCGRenderingIntentDefault);
    CGFloat width = CGImageGetWidth(iref);
    CGFloat height = CGImageGetHeight(iref);
    size_t length = width*height*4;
    uint32_t *pixels = (uint32_t *)malloc(length);
    CGContextRef
context = CGBitmapContextCreate(pixels, width, height, 8, 320*4,
CGImageGetColorSpace(iref), kCGImageAlphaLast |
kCGBitmapByteOrder32Big);
    CGContextTranslateCTM(context, 0.0, height);
    CGContextScaleCTM(context, 1.0, -1.0);
    CGContextDrawImage(context, CGRectMake(0.0, 0.0, width, height), iref);
    CGImageRef outputRef = CGBitmapContextCreateImage(context);
    UIImage *outputImage = [UIImage imageWithCGImage:outputRef];
    
    UIImageWriteToSavedPhotosAlbum(outputImage, nil, nil, nil); 
    
    CGContextRelease(context);
    CGImageRelease(iref);
    CGDataProviderRelease(ref);

4.
CGImageRef UIGetScreenImage();
void SaveScreenImage(NSString *path)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    CGImageRef cgImage = UIGetScreenImage();
        void *imageBytes = NULL;
        if (cgImage == NULL) {
                CGColorSpaceRef colorspace = CGColorSpaceCreateDeviceRGB();
                imageBytes = malloc(320 * 480 * 4);
                CGContextRef
context = CGBitmapContextCreate(imageBytes, 320, 480, 8, 320 * 4,
colorspace, kCGImageAlphaNoneSkipFirst | kCGBitmapByteOrder32Big);
                CGColorSpaceRelease(colorspace);
                for (UIWindow *window in [[UIApplication sharedApplication] windows]) {
                        CGRect bounds = [window bounds];
                        CALayer *layer = [window layer];
                        CGContextSaveGState(context);
                        if ([layer contentsAreFlipped]) {
                                CGContextTranslateCTM(context, 0.0f, bounds.size.height);
                                CGContextScaleCTM(context, 1.0f, -1.0f);
                        }
                        [layer renderInContext:(CGContextRef)context];
                        CGContextRestoreGState(context);
                }
                cgImage = CGBitmapContextCreateImage(context);
                CGContextRelease(context);
        }
    NSData *pngData = UIImagePNGRepresentation([UIImage imageWithCGImage:cgImage]);
    CGImageRelease(cgImage);
        if (imageBytes)
                free(imageBytes);
    [pngData writeToFile:path atomically:YES];
    [pool release];
}

5.
  + (UIImage *)imageWithScreenContents
{
     CGImageRef cgScreen = UIGetScreenImage();
     if (cgScreen) {
         UIImage *result = [UIImage imageWithCGImage:cgScreen];
         CGImageRelease(cgScreen);
         return result;
     }
     return nil;



在程序中如何把两张图片合成为一张图片   
- (UIImage *)addImage:(UIImage *)image1 toImage:(UIImage *)image2 {  
    UIGraphicsBeginImageContext(image1.size);  

    // Draw image1  
    [image1 drawInRect:CGRectMake(0, 0, image1.size.width, image1.size.height)];  

    // Draw image2  
    [image2 drawInRect:CGRectMake(0, 0, image2.size.width, image2.size.height)];  

    UIImage *resultingImage = UIGraphicsGetImageFromCurrentImageContext();  

    UIGraphicsEndImageContext();  

    return resultingImage;  
}

IOS开发几种截屏方法1UIGraphicsBeginImageContextWithOptions(pageViewpageboundssize,YES,zoomScale);[pageViewp
1什么是kvckvckeyvaluecoding,健值编码可以通过key直接访问对象属性的value的方法2使用场景kvc主要是为了让代码变的更简介明了用的比较多的是在后台数据解析,还有访问一些
一、什么是kvo?keyvalueobserving,观察者模式观察者,观察对象属性的变化,当被观察者该属性发生变化时,观察者会接收到通知,可以在回调函数中做相应的处理二、有什么作用?变化处理操作可以
前言在开发中,很多时候我们需要用到定时器实时刷新某个数值。这个时候我们就需要用到定时器,这里,我为大家推荐三种方法,分别是:NSTimer、CADisplayLink、GCD。接下来我就一一介绍它们的
解决IOS开发空字符串的方法实例代码:(Boolean)isEmptyOrNull:(NSString*)str{if(!str){//nullobjectreturntrue;}elseif(str
UITextField是IOS开发中用户交互中重要的一个控件,常被用来做账号密码框,输入信息框等。观察效果图UITextField有以下几种特点:1默认占位文字是灰色的2当光标点上去时,占位文字变为
本篇博文阐述如何开发Ble蓝牙。在蓝牙中的一些常见服务,扫描,以及链接;主蓝牙类文件h主蓝牙类文件mUUID文件蓝牙列表展示的文件一:引入Ble蓝牙的框架<CoreBluetooth/CoreB
本文实例为大家分享了iOS开发微信支付的具体代码,供大家参考,具体内容如下首先我们到微信开放平台,下载相应的SDK。微信的官方文档感觉说的很简单,没有支付宝那么详细,在这里说下集成SDK到我们的工程
iOSAPP开发的发展前景如何?进入新的一年,不仅竞争越来越激烈,而且技术环境也在发生变化。IOSAPP的发展现状主要是受市场影响,一些客观因素影响了整体运营。但是,在了解了市场规模之后,便会迅速进行
公司来了大牛是绝好的学习机会今天分享了我们一个代码规范比如UITableViewCell1首先根据这个cell需要的数据源建一个数据model,只针对于该cell好处:数据独立化2对于cell里面的实