IOS螢幕截圖—普通截圖

來源:互聯網
上載者:User
============================================================博文原創,轉載請聲明出處電子咖啡(原id藍岩)============================================================

分享一段螢幕的代碼,能夠普通的button,image,等。

對於截取網路攝影機的映像,比較複雜,因為網路攝影機是通過opengl渲染到view上面的,通過下面的代碼並不能截取。

就像我們遠程登入了另一台windows電腦時候,能看到對方的案頭等,但對方的視頻我們並不能看到。

關於截取opengl view的方法,我會在另一篇部落格中記錄: 

IOS螢幕---Opengl截屏

我已經在我的工程中測試過,可以使用

//普通螢幕截取

首先引入頭:

#include <QuartzCore/QuartzCore.h>

/**@view  the view you want to take screen shot*/-(UIImage*)getNormalImage:(UIView*)view{    UIGraphicsBeginImageContext(CGSizeMake(320,480));    CGContextRef context = UIGraphicsGetCurrentContext();    [view.layer renderInContext:context];    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();    UIGraphicsEndImageContext();    return image;}

//將UIIamge儲存到相簿中

//store the image to the iphone photo albumUIImage *image =nil;// init your imageUIImageWriteToSavedPhotosAlbum(image, self, nil, nil);

//儲存UIImage到本地磁碟,包括png和jpeg格式

//store the image to the app Document-(void)saveToDisk:(UIImage*) image{        NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];    // If you go to the folder below, you will find those picturesNSLog(@"%@",docDir);    NSLog(@"saving png");NSString *pngFilePath = [NSString stringWithFormat:@"%@/test%f.png",docDir,[NSDate timeIntervalSinceReferenceDate]];NSData *data1 = [NSData dataWithData:UIImagePNGRepresentation(image)];[data1 writeToFile:pngFilePath atomically:YES];    NSLog(@"saving jpeg");NSString *jpegFilePath = [NSString stringWithFormat:@"%@/test%f.jpeg",docDir,[NSDate timeIntervalSinceReferenceDate]];NSData *data2 = [NSData dataWithData:UIImageJPEGRepresentation(image, 1.0f)];//1.0f = 100% quality[data2 writeToFile:jpegFilePath atomically:YES];    NSLog(@"saving image done");}

ref:

Download an Image and Save it as PNG or JPEG in iPhone SDK ‹ ObjectGraph Blog

iphone - Why do I get 'No -renderInContext: method found' warning? - Stack Overflow

iphone - Programmatically take a screenshot combining OpenGL and UIKit elements - Stack Overflow

iPhone – saving OpenGL ES content to the Photo Album | BIT-101

opengl es - Why is glReadPixels() failing in this code in iOS 6.0? - Stack Overflow

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.