周精益分享 - Swift入门专题

『图像和滤镜』 - 图像选择器

我们可以使用以下常规的图像获取方式

Alt text

图库与相册

Alt text

系统自带

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
let sheet = UIAlertController(title: "图片选择", message: "简单版的三种选择", preferredStyle: .ActionSheet)
// 判断设备是否支持相机(iPod & Simulator)
if (UIImagePickerController.isSourceTypeAvailable(.Camera)) {
sheet.addAction(UIAlertAction.init(title: "Camera", style: .Default, handler: { _ in
self.showPhotoes(.Camera)
}))
}
sheet.addAction(UIAlertAction.init(title: "PhotoLibrary", style: .Default, handler: { _ in
self.showPhotoes(.PhotoLibrary)
}))
sheet.addAction(UIAlertAction.init(title: "SavedPhotosAlbum", style: .Default, handler: { _ in
self.showPhotoes(.SavedPhotosAlbum)
}))
sheet.addAction(UIAlertAction.init(title: "Cancel", style: .Cancel, handler: nil))
presentViewController(sheet, animated: true, completion: nil)
}
1
2
3
4
5
6
7
8
func showPhotoes(source: UIImagePickerControllerSourceType) {
let controller = UIImagePickerController()
controller.delegate = self
controller.sourceType = source
controller.allowsEditing = source == .SavedPhotosAlbum ? true:false
self.presentViewController(controller, animated: true, completion: nil)
}

UIImagePickerController的代理

1
2
3
4
5
6
7
8
func imagePickerControllerDidCancel(picker: UIImagePickerController) {
dismissViewControllerAnimated(true, completion: nil)
}
// 非常坑,这个方法废弃了但代码提示只有它
// func imagePickerController(picker: UIImagePickerController, didFinishPickingImage image: UIImage, editingInfo: [String : AnyObject]?) {
// }
func imagePickerController(picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : AnyObject]) {
}

#####info字典介绍

1
2
3
4
5
6
7
8
9
10
UIImagePickerControllerMediaType: String
UIImagePickerControllerOriginalImage: UIImage
UIImagePickerControllerEditedImage: UIImage
UIImagePickerControllerCropRect: NSValue -> CGRect
// MediaURL只为视频提供
UIImagePickerControllerMediaURL: NSURL
// LivePhoto是一张图片,保留那个moment的前后动作和声音
UIImagePickerControllerLivePhoto: String
// 摄像摄影时返回media的信息字典
UIImagePickerControllerMediaMetadata: NSDictionary

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// SavedPhotosAlbum 的 info 示例
▿ 5 elements
▿ [0] : 2 elements
- .0 : "UIImagePickerControllerCropRect"
▿ [1] : 2 elements
- .0 : "UIImagePickerControllerOriginalImage"
▿ [2] : 2 elements
- .0 : "UIImagePickerControllerReferenceURL"
- .1 : assets-library://asset/asset.JPG?id=99D53A1F-FEEF-40E1-8BB3-7DD55A43C8B7&ext=JPG
▿ [3] : 2 elements
- .0 : "UIImagePickerControllerMediaType"
- .1 : public.image
▿ [4] : 2 elements
- .0 : "UIImagePickerControllerEditedImage"

自定义

Alt text

遍历相册的所有图片

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// AssetsLibrary.framework
// ALAssetsLibrary 的使用,但是它慢慢的被放弃了
func loadLocalPhotoes(){
var countOne = 0
//ALAssetsGroupSavedPhotos表示只读取相机胶卷(ALAssetsGroupAll则读取全部相簿)
assetsLibrary.enumerateGroupsWithTypes(ALAssetsGroupSavedPhotos, usingBlock: {
(group: ALAssetsGroup!, stop) in
print("is goin")
if group != nil {
let assetBlock : ALAssetsGroupEnumerationResultsBlock = {
(result: ALAsset!, index: Int, stop) in
if result != nil {
self.assets.append(result)
countOne++
}
}
group.enumerateAssetsUsingBlock(assetBlock)
print("assets:\(countOne)")
self.startChangeLocalImages(0)
}
}, failureBlock: { (fail) in
print(fail)
})
}
// 展现本地图片
func startChangeLocalImages(var index: Int){
if index==assets.count {
index = 0
}
let myAsset = assets[index]
let image = UIImage(CGImage:myAsset.thumbnail().takeUnretainedValue())
self.backImageView.image = image
let popTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1 * Double(NSEC_PER_SEC)))
dispatch_after(popTime, dispatch_get_main_queue()) {
self.startChangeLocalImages(index+1)
}
}

iOS9 开始使用新库
PHPhotoLibrary