iOS 截屏分享(包含狀態欄與不包含狀態欄)



iOS8以上的新方法PhotoKit

監聽截圖相冊變化,取最后一張圖片:http://www.hangge.com/blog/cache/detail_1515.html

PhotoKit 獲取本機相冊列表 PHAssetCollection有一個名為 Screenshots的智能相冊,獲取這里面最新的一張圖片,即可獲取用戶剛剛截屏的圖片

自己截取屏幕,兼容iOS7-iOS10:帶狀態欄

  • OTScreenshotHelper oc 開源庫
    https://github.com/OpenFibers/OTScreenshotHelper

  • 直接使用 OTScreenshotHelper 截圖效果如下:
    不知道為什么頭部和底部的view是透明的,上下兩個view都設置了毛玻璃效果,但是截圖出來view背景透明
    image_1bludrnbpre877i1ehfa4m19td9.png-221.9kB

  • 目前解決方案是不帶狀態欄的,所以試過各種方案之后還是兩個方案融合在一起:使用OTScreenshotHelper截圖狀態欄, 使用不帶狀態欄方案截取主體部分

1. 剛剛拷貝源碼到swift項目

  • UIView+OTStatusBarReference.m
#import "UIView+ComOpenThreadOTScreenshotHelperStatusBarReference.h"
#import "ComOpenThreadOTScreenshotHelperSwizzleHelper.h"
// 這一行需要移動到 import 語句下面,不然會報庫找不到
static UIView *statusBarInstance = nil;
  • OTScreenShotHelper.h
把#import <Foundation/Foundation.h> 
增加:
#import <UIKit/UIKit.h>
解決UIView 提示找不到的問題

2. OTScreenShotHelper.h 中暴露mergeStatusBarToContext方法

這個方法在OTScreenShotHelper 其實已經實現了,就是沒有暴露給外面調用

+ (void)mergeStatusBarToContext:(CGContextRef)context rect:(CGRect)rect screenshotOrientation:(UIInterfaceOrientation)o;

3. 在現有截圖代碼位置調用mergeStatusBarToContext

for subWindow in (UIApplication.shared.windows) {
context.saveGState()
context.translateBy(x: subWindow.center.x, y: subWindow.center.y)
context.concatenate(subWindow.transform)
context.translateBy(x: -subWindow.bounds.size.width * subWindow.layer.anchorPoint.x,
y: -subWindow.bounds.size.height * subWindow.layer.anchorPoint.y)
if (orientation == UIInterfaceOrientation.landscapeLeft) {
context.rotate(by: CGFloat(M_PI_2))
context.translateBy(x: 0, y: -imageSize.width)
}
else if (orientation == UIInterfaceOrientation.landscapeRight) {
context.rotate(by: -CGFloat(M_PI_2))
context.translateBy(x: -imageSize.width, y: 0)
}
else if (orientation == UIInterfaceOrientation.portraitUpsideDown) {
context.rotate(by: CGFloat(M_PI))
context.translateBy(x: -imageSize.width, y: -imageSize.height)
}
if ( subWindow.responds(to: #selector(UIWindow.drawHierarchy(in:afterScreenUpdates:))) ){
subWindow.drawHierarchy(in: subWindow.bounds, afterScreenUpdates: true)
} else {
subWindow.layer.render(in: context)
}
context.restoreGState()

// 這部分是截圖帶有statusbar的關鍵,在這里把狀態欄截圖動態添加到了原有圖片中,在未帶狀態欄截圖代碼如下位置添加如下代碼即可
let statusBarRect = CGRect(x: 0, y: 0, width: AppWidth, height: 20)
OTScreenshotHelper.mergeStatusBar(to: context, rect: statusBarRect, screenshotOrientation: orientation)
// End

}

let image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return image

未帶狀態欄截圖代碼參見:
https://gist.github.com/buoge/a94798cd49854a569fdc32825c015167


自己截取屏幕,兼容iOS7-iOS10:不帶狀態欄

image_1blue6gp9u7d1svtr38hug1m9im.png-119.7kB

創建兩個UIImage
UIImage *image1 = [UIImage imageNamed:@"iOSDevTip"];
UIImage *image2 = [UIImage imageNamed:@"CodePush"];
創建UIImage的方法有很多種,我們就簡單的通過imageNamed:方法來創建。

合並之后的size
CGSize size = CGSizeMake(image1.size.width + image2.size.width, image1.size.height);

合並兩個UIImage,需要計算合並之后的size。假設這兩個UIImage的高度是是相同的,把他們的寬度相加,得到合並之后的UIImage的size。
合並方法有了UIImage和size接下來就是把兩個UIImage合並,方法如下:

UIGraphicsBeginImageContext(size);
[image1 drawInRect:CGRectMake(0, 0, image1.size.width, size.height)];
[image2 drawInRect:CGRectMake(image1.size.width, 0, image2.size.width, size.height)];
UIImage *togetherImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM