公司測(cè)試反應(yīng),有時(shí)候出現(xiàn)問(wèn)題來(lái)不及截圖..所以寫(xiě)個(gè)搖一搖截圖
方法寫(xiě)在UIViewController的分類(lèi)里,無(wú)需調(diào)用,立即生效
#import "UIViewController+ShakeAndCutter.h"
@implementation UIViewController (ShakeAndCutter)
- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
[self snapshot];
}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
NSLog(@"End");
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event{
NSLog(@"Cancel");
}
- (void)snapshot
{
// 1. 開(kāi)啟圖像上下文[必須先開(kāi)開(kāi)啟上下文再執(zhí)行第二步鬼雀,順序不可改變]
UIGraphicsBeginImageContext(self.view.bounds.size);
// 2. 獲取上下文
CGContextRef context = UIGraphicsGetCurrentContext();
// 3. 將當(dāng)前視圖圖層渲染到當(dāng)前上下文
[self.view.layer renderInContext:context];
// 4. 從當(dāng)前上下文獲取圖像
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
// 5. 關(guān)閉圖像上下文
UIGraphicsEndImageContext();
// 6. 保存圖像至相冊(cè)
UIImageWriteToSavedPhotosAlbum(image, self, @selector(image:didFinishSavingWithError:contextInfo:), nil);
}
#pragma mark 保存完成后調(diào)用的方法[格式固定]
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo
{
if (error) {
NSLog(@"error-%@", error.localizedDescription);
}else{
NSLog(@"保存成功");
}
}
需要注意:在plist中添加
Privacy - Photo Library Usage Description
訪問(wèn)相冊(cè)提醒