xcode Swift 3:在不使用 UIImagePickerController 的情况下从照片/相机胶卷加载照片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44877195/
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 3: Load photos from Photos/Camera Roll without using UIImagePickerController
提问by Spiral1ng
This question has been asked beforebut there were no answers for Swift 3. I am looking for the same solution which I am stuck for the past 3 weeks.
之前有人问过这个问题,但 Swift 3 没有答案。我正在寻找与过去 3 周卡住的相同解决方案。
I have done my research and watched numerous Youtube videos about loading images from Photos/Camera Roll into the app using UIImagePickerController but I want to access the photos without user actions.
我已经完成了我的研究并观看了许多关于使用 UIImagePickerController 从照片/相机胶卷加载图像到应用程序的 Youtube 视频,但我想在没有用户操作的情况下访问照片。
I want to read a series of photos from camera roll and put them in a photo slide to show them one by one. How can I access these photos without UIImagePickerController?
我想从相机胶卷中读取一系列照片并将它们放在照片幻灯片中以一张一张地展示。如何在没有 UIImagePickerController 的情况下访问这些照片?
回答by Rajan Maheshwari
You can use the Photos
framework to fetch the photos from CameraRoll/Photos.
您可以使用该Photos
框架从 CameraRoll/Photos 中获取照片。
Here is the version of Swift 3code.
这是Swift 3代码的版本。
Import the photos framework
导入照片框架
import Photos
//Array of PHAsset type for storing photos
var images = [PHAsset]()
Use this function to fetch the photos, somewhere in viewDidLoad
or on your action wherever you want to fetch the photos.
使用此功能viewDidLoad
在您想要获取照片的任何位置或动作中的某个位置获取照片。
func getImages() {
let assets = PHAsset.fetchAssets(with: PHAssetMediaType.image, options: nil)
assets.enumerateObjects({ (object, count, stop) in
// self.cameraAssets.add(object)
self.images.append(object)
})
//In order to get latest image first, we just reverse the array
self.images.reverse()
// To show photos, I have taken a UICollectionView
self.photosCollectionView.reloadData()
}
Rest are UICollectionView Datasource and Delegates. See the cellForItem
datasource method on How to show the image.
其余的是 UICollectionView 数据源和委托。请参阅cellForItem
有关如何显示图像的数据源方法。
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return images.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotoCollectionViewCell", for: indexPath) as! PhotoCollectionViewCell
let asset = images[indexPath.row]
let manager = PHImageManager.default()
if cell.tag != 0 {
manager.cancelImageRequest(PHImageRequestID(cell.tag))
}
cell.tag = Int(manager.requestImage(for: asset,
targetSize: CGSize(width: 120.0, height: 120.0),
contentMode: .aspectFill,
options: nil) { (result, _) in
cell.photoImageView?.image = result
})
return cell
}
Adjust the below delegates as per your need.
根据您的需要调整以下代表。
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
let width = self.view.frame.width * 0.32
let height = self.view.frame.height * 0.179910045
return CGSize(width: width, height: height)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
return 2.5
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 5, left: 5, bottom: 5, right: 5)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
return 0
}
Make sure to keep the photos permission ON. If you click Don't Allow, then you have to manage the authorization too using PHPhotoLibrary.authorizationStatus()
确保保持照片权限ON。如果您单击不允许,那么您也必须使用PHPhotoLibrary.authorizationStatus()
You can read more about Photosframework.
您可以阅读有关照片框架的更多信息。