指定截屏代碼實(shí)現(xiàn)
全屏截圖效果
指定區(qū)域截屏效果
這里先上代碼渣叛,代碼后面有相關(guān)方法的解釋
第一種方法
代碼下載
/**
創(chuàng)建一個基于位圖的上下文(context),并將其設(shè)置為當(dāng)前上下文(context)
@param size 參數(shù)size為新創(chuàng)建的位圖上下文的大小能真。它同時是由UIGraphicsGetImageFromCurrentImageContext函數(shù)返回的圖形大小
@param opaque 透明開關(guān)呆抑,如果圖形完全不用透明惊楼,設(shè)置為YES以優(yōu)化位圖的存儲抱既,我們得到的圖片背景將會是黑色僻澎,使用NO涩盾,表示透明,圖片背景色正常
@param scale 縮放因子 iPhone 4是2.0敷硅,其他是1.0功咒。雖然這里可以用[UIScreen mainScreen].scale來獲取,但實(shí)際上設(shè)為0后绞蹦,系統(tǒng)就會自動設(shè)置正確的比例了
*/
UIGraphicsBeginImageContextWithOptions([[UIScreen mainScreen] bounds].size, YES, 0.0);
UIView * view = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
//把控制器View的內(nèi)容繪制到上下文當(dāng)中.
//layer是不能夠直接繪制的.要用渲染的方法才能夠讓它繪制到上下文當(dāng)中力奋。UIGraphicsGetCurrentContext()
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
//從上下文當(dāng)中生成一張圖片
UIImage *viewImage = UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文.
UIGraphicsEndImageContext();
/*
//image:要把圖片轉(zhuǎn)成二進(jìn)制流,compressionQuality:可壓縮質(zhì)量.
NSData*data =UIImagePNGRepresentation(viewImage);
*/
//上面我們獲得了一個全屏的截圖幽七,下邊的方法是對這個圖片進(jìn)行裁剪景殷。
CGImageRef imageRef =viewImage.CGImage;
//這里要特別注意,這里的寬度 CGImageGetWidth(imageRef) 是圖片的像素寬(高度同解)澡屡,所以計算截圖區(qū)域時需要按比例來猿挚;
//這里舉的例子是寬高屏幕1/2位置在中心
CGRect rect = CGRectMake(CGImageGetWidth(imageRef)/4, CGImageGetHeight(imageRef)/2-CGImageGetWidth(imageRef)/2, CGImageGetWidth(imageRef)/2, CGImageGetWidth(imageRef)/2);//這里可以設(shè)置想要截圖的區(qū)域
CGImageRef imageRefRect =CGImageCreateWithImageInRect(imageRef, rect);
UIImage *sendImage =[[UIImage alloc] initWithCGImage:imageRefRect];
第二種方法
代碼下載
//設(shè)置邊框?qū)挾? CGFloat borderWH = 5;
//圖片寬度(像素)
CGFloat imageWH = self.baseImageView.image.size.width;
NSLog(@"=========%f",imageWH);
//開啟一個位圖上下文(W = imageWH + 2 * border H = imageWH + 2 * border)
CGSize size =CGSizeMake(imageWH + 2*borderWH, imageWH + 2*borderWH);
UIGraphicsBeginImageContextWithOptions(size,NO,0);
//繪制一個圓形的形狀.
UIBezierPath*path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(0,0, size.width, size.height)];
//邊框顏色
UIColor *color = [UIColor redColor];
[color set];
[path fill];
//設(shè)置一個小圓,并設(shè)置成裁剪區(qū)域
path = [UIBezierPath bezierPathWithOvalInRect:CGRectMake(borderWH, borderWH, imageWH, imageWH)];
//把路徑設(shè)置成裁剪區(qū)域
[path addClip];
//把圖片繪制到上下文當(dāng)中.
[self.baseImageView.image drawAtPoint:CGPointMake(borderWH, borderWH)];
//從上下文當(dāng)中生成一張圖片
UIImage*newImage =UIGraphicsGetImageFromCurrentImageContext();
//關(guān)閉上下文.
UIGraphicsEndImageContext();
self.bottonImageView.image = newImage;
相關(guān)知識詳解
UIGraphicsBeginImageContext
創(chuàng)建一個基于位圖的上下文(context)
,并將其設(shè)置為當(dāng)前上下文(context)
。方法聲明如下:
void UIGraphicsBeginImageContext(CGSize size);
參數(shù)size
為新創(chuàng)建的位圖上下文的大小驶鹉。它同時是UIGraphicsGetImageFromCurrentImageContext
函數(shù)返回的圖形大小绩蜻。
該函數(shù)的功能同UIGraphicsBeginImageContextWithOptions
的功能相同,相當(dāng)與UIGraphicsBeginImageContextWithOptions
的opaque
參數(shù)為NO室埋,scale
因子為1.0办绝。
UIGraphicsBeginImageContextWithOptions
函數(shù)原型為:
void UIGraphicsBeginImageContextWithOptions(CGSize size, BOOL opaque, CGFloat scale);
size——同UIGraphicsBeginImageContext
opaque—透明開關(guān),如果圖形完全不用透明姚淆,設(shè)置為YES以優(yōu)化位圖的存儲孕蝉。
scale—–縮放因子 iPhone 4是2.0,其他是1.0腌逢。雖然這里可以用[UIScreen mainScreen].scale
來獲取降淮,但實(shí)際上設(shè)為0后,系統(tǒng)就會自動設(shè)置正確的比例了搏讶。
其他的截屏方法
第二種
這是在比較常見的截圖方法佳鳖,不過不支持Retina屏幕。
UIGraphicsBeginImageContext(self.view.frame.size);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
第三種
從iPhone 4媒惕、iPod Touch 4開始系吩,Apple逐漸采用Retina屏幕,于是在iOS 4的SDK中我們有了吓笙,上面的截圖方法也自然變成了這樣。
UIGraphicsBeginImageContextWithOptions(self.view.frame.size, NO, 0.0);
[self.view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
第四種
或許你會說有時Hook的是一個按鈕的方法巾腕,用第三個方法的話面睛,根本找不到view來傳值絮蒿,不過還好,iOS 7又提供了一些UIScreen的API叁鉴。
UIView * view = [[UIScreen mainScreen] snapshotViewAfterScreenUpdates:YES];
UIGraphicsBeginImageContextWithOptions(view.frame.size, NO, 0.0);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;