标签:
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
import UIKit import AssetsLibrary class ViewController : UICollectionViewController { //资源库管理类 var assetsLibrary = ALAssetsLibrary () //保存照片集合 var assets = [ ALAsset ]() override func viewDidLoad() { super .viewDidLoad() var countOne = 0 //ALAssetsGroupSavedPhotos表示只读取相机胶卷(ALAssetsGroupAll则读取全部相簿) assetsLibrary.enumerateGroupsWithTypes( ALAssetsGroupSavedPhotos , usingBlock: { (group: ALAssetsGroup !, stop) in println ( "is goin" ) if group != nil { var assetBlock : ALAssetsGroupEnumerationResultsBlock = { (result: ALAsset !, index: Int , stop) in if result != nil { self .assets.append(result) countOne++ } } group.enumerateAssetsUsingBlock(assetBlock) println ( "assets:\(countOne)" ) //collectionView网格重载数据 self .collectionView?.reloadData() } }, failureBlock: { (fail) in println (fail) }) } // CollectionView行数 override func collectionView(collectionView: UICollectionView , numberOfItemsInSection section: Int ) -> Int { return assets.count; } // 获取单元格 override func collectionView(collectionView: UICollectionView , cellForItemAtIndexPath indexPath: NSIndexPath ) -> UICollectionViewCell { // storyboard里设计的单元格 let identify: String = "DesignViewCell" // 获取设计的单元格,不需要再动态添加界面元素 let cell = self .collectionView?.dequeueReusableCellWithReuseIdentifier( identify, forIndexPath: indexPath) as ! UICollectionViewCell //取缩略图 var myAsset = assets[indexPath.item] var image = UIImage ( CGImage :myAsset.thumbnail().takeUnretainedValue()) // 从界面查找到控件元素并设置属性 (cell.contentView.viewWithTag(1) as ! UIImageView ).image = image return cell } // 单元格点击响应 override func collectionView(collectionView: UICollectionView , didSelectItemAtIndexPath indexPath: NSIndexPath ) { var myAsset = assets[indexPath.item] //这里不使用segue跳转(建议用segue跳转) var detailViewController = UIStoryboard (name: "Main" , bundle: nil ) .instantiateViewControllerWithIdentifier( "detail" ) as ! ImageDetailViewController detailViewController.myAsset = self .assets[indexPath.row] // navigationController跳转到detailViewController self .navigationController!.pushViewController(detailViewController, animated: true ) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
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
|
import UIKit import AssetsLibrary class ImageDetailViewController : UIViewController { //选中的图片资源 var myAsset: ALAsset ! //用于显示图片信息 @IBOutlet weak var textView: UITextView ! //用于显示原图 @IBOutlet weak var imageView: UIImageView ! override func viewDidLoad() { super .viewDidLoad() //获取文件名 var representation = myAsset.defaultRepresentation() self .title = representation.filename() //获取图片信息 textView.text = "日期:\(myAsset.valueForProperty(ALAssetPropertyDate))\n" + "类型:\(myAsset.valueForProperty(ALAssetPropertyType))\n" + "位置:\(myAsset.valueForProperty(ALAssetPropertyLocation))\n" + "时长:\(myAsset.valueForProperty(ALAssetPropertyDuration))\n" + "方向:\(myAsset.valueForProperty(ALAssetPropertyOrientation))" //获取原图 var imageBuffer = UnsafeMutablePointer < UInt8 >.alloc( Int (representation.size())) var bufferSize = representation.getBytes(imageBuffer, fromOffset: Int64 (0), length: Int (representation.size()), error: nil ) var data = NSData (bytesNoCopy:imageBuffer ,length:bufferSize, freeWhenDone: true ) imageView.image = UIImage (data: data) } override func didReceiveMemoryWarning() { super .didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } } |
Swift - 使用ALAssetsLibrary获取相簿里所有图片,视频(附样例)
标签:
原文地址:http://www.cnblogs.com/Free-Thinker/p/4843339.html