xcode swift:如何在不使用 UIImagePickerController 的情况下从照片库加载照片?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35115117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
swift: How to load photos from photo library without using UIImagePickerController?
提问by Joey Yi Zhao
I am using swift to load images from photo library. I know that I can use UIImagePickerController to let user to choose the images from camera roll but I want to access photos without user actions. For example, I want to read a series of photos which are from camera roll and put them in a photo slide to show them one by one. How can I access these photos without UIImagePickerController?
我正在使用 swift 从照片库中加载图像。我知道我可以使用 UIImagePickerController 让用户从相机胶卷中选择图像,但我想在没有用户操作的情况下访问照片。例如,我想读取一系列来自相机胶卷的照片并将它们放在照片幻灯片中以一张一张地展示。如何在没有 UIImagePickerController 的情况下访问这些照片?
采纳答案by curious
You can access photo library using Photosframework(for iOS 8+). Take a look at thisexample. In AAPLRootListViewController's code if awakeFromNib method you can see how to create PHFetchResult object to retrieve specific photos data. Then, in AAPLAssetGridViewController, PHAsset objects(from PHFetchResult) are used for retrieving actual image data. Take a look at method
您可以使用照片框架(适用于 iOS 8+)访问照片库。看看这个例子。在 AAPLRootListViewController 的代码 ifawakeFromNib 方法中,您可以看到如何创建 PHFetchResult 对象来检索特定的照片数据。然后,在 AAPLAssetGridViewController 中,PHAsset 对象(来自 PHFetchResult)用于检索实际图像数据。看方法
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
The code is written in Objective-C rather than Swift, but you could do the same using Swift. Ask questions if any.
代码是用 Objective-C 而不是 Swift 编写的,但您可以使用 Swift 来做同样的事情。如有问题,请提问。