注意
在iOS中訪問相冊济榨,首先我們得獲取用戶授權(quán),在iOS10之前不用再Info.plist寫參數(shù)绿映。
在iOS10 之后要自定義彈出的信息
Privacy - Photo Library Usage Description
Privacy - Camera Usage Description
獲取權(quán)限
/// 獲取權(quán)限
///
/// - Parameter accessBlock: 閉包回調(diào)
func accessCameraAuthority(accessBlock:AccessBlock!)
{
let authStatus = AVCaptureDevice.authorizationStatus(forMediaType: AVMediaTypeVideo)
if authStatus == AVAuthorizationStatus.notDetermined {
// 請求授權(quán)
AVCaptureDevice.requestAccess(forMediaType: AVMediaTypeAudio, completionHandler: { finish in
if finish {
if (accessBlock) != nil
{
accessBlock(true)
}
}
else
{
if (accessBlock) != nil
{
accessBlock(false)
}
}
})
}
else if authStatus == AVAuthorizationStatus.authorized
{
// 授權(quán)成功
if (accessBlock) != nil
{
accessBlock(true)
}
}
else
{
// 用戶拒絕授權(quán)
if (accessBlock) != nil
{
accessBlock(false)
}
}
}
當(dāng)返回true擒滑,我們就可以有訪問權(quán)限了。
在iOS中截屏的方法如下:
/// 截屏
///
/// - Parameters:
/// - view: 要截屏的view
/// - Returns: 一個UIImage
func cutImageWithView(view:UIView) -> UIImage
{
// 參數(shù)①:截屏區(qū)域 參數(shù)②:是否透明 參數(shù)③:清晰度
UIGraphicsBeginImageContextWithOptions(view.frame.size, true, UIScreen.main.scale)
view.layer.render(in: UIGraphicsGetCurrentContext()!)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return image;
}
這個方法是獲取當(dāng)前view的內(nèi)容叉弦,size的大小要巧妙的設(shè)置丐一,比如你的view超出了看到的屏幕,你使用截圖下來卸奉,
就是一部分回事黑色的區(qū)域钝诚。你截取當(dāng)前能看到的颖御,就widow就可以了榄棵。
我們在使用很多APP的時候會截出很長的圖,這個時候我們就用巧取得方式來獲取潘拱,方法如下:
/// 截屏
///
/// - Parameters:
/// - view: 要截屏的view
/// - Returns: 一個UIImage
func cutFullImageWithView(scrollView:UIScrollView) -> UIImage
{
// 記錄當(dāng)前的scrollView的偏移量和坐標(biāo)
let currentContentOffSet:CGPoint = scrollView.contentOffset
let currentFrame:CGRect = scrollView.frame;
// 設(shè)置為zero和相應(yīng)的坐標(biāo)
scrollView.contentOffset = CGPoint.zero
scrollView.frame = CGRect.init(x: 0, y: 0, width: scrollView.frame.size.width, height: scrollView.frame.size.height)
// 參數(shù)①:截屏區(qū)域 參數(shù)②:是否透明 參數(shù)③:清晰度
UIGraphicsBeginImageContextWithOptions(scrollView.contentSize, true, UIScreen.main.scale)
scrollView.layer.render(in: UIGraphicsGetCurrentContext()!)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
// 重新設(shè)置原來的參數(shù)
scrollView.contentOffset = currentContentOffSet
scrollView.frame = currentFrame
UIGraphicsEndImageContext();
return image;
}
這里是保證他是scrllView疹鳄,來切換相應(yīng)的offset和frame,來截取屏幕芦岂。有一點(diǎn)要注意瘪弓,比如你的是tableView,下面的數(shù)據(jù)中的圖片沒有加載出來禽最,也是肯定不會顯示的腺怯。
接下來就是把截圖保存到相冊袱饭,下面是保存到系統(tǒng)相冊:
func writeImageToAlbum(image:UIImage)
{
UIImageWriteToSavedPhotosAlbum(image, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
}
func image(image: UIImage, didFinishSavingWithError error: NSError?, contextInfo:UnsafeRawPointer)
{
if let e = error as NSError?
{
print(e)
}
else
{
UIAlertController.init(title: nil,
message: "保存成功!",
preferredStyle: UIAlertControllerStyle.alert).show(self, sender: nil);
}
}
寫入到指定的相冊呛占,這里看到的是先寫入相冊虑乖,再移動到指定的相冊
// 看有沒有指定的相冊
let library = ALAssetsLibrary.init()
let albumGroups = NSMutableArray()
library.enumerateGroups(withTypes: ALAssetsGroupType(ALAssetsGroupAll), using:
{
oneGroup,pointer in
albumGroups.add(oneGroup! as ALAssetsGroup)
},
failureBlock:
{
error in
})
var haveCustomAlbum = false
for albumGroup in albumGroups
{
let groupName:String = (albumGroup as! ALAssetsGroup).value(forProperty: ALAssetsGroupPropertyName) as! String
print(groupName)
if groupName == "指定相冊"
{
haveCustomAlbum = true
}
}
如果有指定相冊直接寫入沒有就去創(chuàng)建
// 這里是創(chuàng)建相冊
if !haveCustomAlbum
{
library.addAssetsGroupAlbum(withName: "指定相冊",
resultBlock:
{alAsetGroup in
if alAsetGroup != nil
{
print("創(chuàng)建相冊成功");
}
},
failureBlock:
{error in
print("創(chuàng)建相冊失敗");
})
}
下面是寫入到指定的相冊,下面的image晾虑,group和library都是上面獲取到的
func writeImageToCustomAlbumFinish(image:UIImage,library:ALAssetsLibrary,alAsetGroup:ALAssetsGroup)
{
// 首先寫入到寫到相冊 這里也可以用PHPhotoLibrary來做
library.writeImage(toSavedPhotosAlbum: image.cgImage, orientation:.up, completionBlock:
{ url,error in
if (error != nil)
{
// 取到asset
library.asset(for: url, resultBlock:
{
aAlAset in
if aAlAset != nil
{
// 添加到group
alAsetGroup.add(aAlAset)
}
},
failureBlock:
{
error in
})
}
})
}
拼圖
func combinTwoImage(image1:UIImage,image2:UIImage) -> UIImage
{
let width = max(image1.size.width, image2.size.width)
let height = image1.size.height + image2.size.height
let offScreenSize = CGSize.init(width: width, height: height)
UIGraphicsBeginImageContext(offScreenSize);
let rect = CGRect.init(x:0, y:0, width:width, height:image1.size.height)
image1.draw(in: rect)
let rect2 = CGRect.init(x:0, y:image1.size.height, width:width, height:image2.size.height)
image1.draw(in: rect2)
let image:UIImage = UIGraphicsGetImageFromCurrentImageContext()!
UIGraphicsEndImageContext();
return image;
}