gpt4 book ai didi

ios - swift 3 : Load photos from Photos/Camera Roll without using UIImagePickerController

转载 作者:搜寻专家 更新时间:2023-10-30 22:03:21 24 4
gpt4 key购买 nike

This question has been asked before但是没有针对 Swift 3 的答案。我正在寻找与过去 3 周一样的解决方案。

我已经完成研究并观看了大量关于使用 UIImagePickerController 从照片/相机胶卷将图像加载到应用程序的 Youtube 视频,但我想在没有用户操作的情况下访问照片。

我想从相机胶卷中读取一系列照片,然后将它们放入照片幻灯片中逐张展示。如何在没有 UIImagePickerController 的情况下访问这些照片?

最佳答案

您可以使用 Photos 框架从 CameraRoll/Photos 中获取照片。

这是 Swift 3 代码的版本。

导入照片框架

import Photos

//Array of PHAsset type for storing photos
var images = [PHAsset]()

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()
}

其余的是 UICollectionView 数据源和委托(delegate)。请参阅有关如何显示图像的 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
}

根据您的需要调整以下委托(delegate)。

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
}

确保将照片权限设为ON。如果您单击“不允许”,那么您也必须使用 PHPhotoLibrary.authorizationStatus()

管理授权

您可以阅读更多关于 Photos 的信息框架。

关于ios - swift 3 : Load photos from Photos/Camera Roll without using UIImagePickerController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44877195/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com