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背景透明
-
目前解決方案是不帶狀態欄的,所以試過各種方案之后還是兩個方案融合在一起:使用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:不帶狀態欄
-
swift版截圖並展示
https://gist.github.com/buoge/a94798cd49854a569fdc32825c015167 -
這個代碼有個問題在播放視頻的頁面播放,截取出來是黑屏
-
參考原文oc版
http://blog.csdn.net/hitwhylz/article/details/38386979?utm_source=tuicool&utm_medium=referral -
iOS 7.0之后有系統事件
原理簡單的說下:iOS 7.0之后加入了一個系統通知
UIApplicationUserDidTakeScreenshotNotification 截屏觸發的通知 -
hangge擴展閱讀 圖片保存到本地
http://www.hangge.com/blog/cache/detail_1102.html -
SwViewCapture 沒有考慮到mkwebview,webview截圖,截圖閃屏等等問題
http://blog.startry.com/2016/02/24/Screenshots-With-SwViewCapture -
ImageHelper 可以很好的處理截圖問題
https://github.com/melvitax/ImageHelper
Screenshot,Creates an image from a UIView:
UIImage(fromView view: UIView) -
兩張圖片合成一張
http://www.superqq.com/blog/2015/08/05/multiple-uiimage-merged/
創建兩個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();