iOS   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了ios – Swift 3:在不使用UIImagePickerController的情况下从照片/相机胶卷加载照片大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_489_2@

概述

This question has been asked before但没有Swift 3的答案.我正在寻找过去3周我遇到的相同解决方案. 我已经完成了我的研究并观看了许多关于使用UIImagePickerController将照片/相机卷中的图像加载到应用程序中的Youtube视频,但我想在没有用户操作的情况下访问这些照片. 我想从相机胶卷中读取一系列照片并将它们放在照片幻灯片中,逐一显示.如何
@H_489_2@
@H_489_2@ @H_489_2@
@H_489_2@
This question has been asked before但没有Swift 3的答案.我正在寻找过去3周我遇到的相同解决方案.

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

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

@H_489_2@

解决方法

您可以使用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数据源和代理.请参阅如何显示图像的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
}

根据您的需要调整以下代表.

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,minimumLinespacingForSectionAt section: int) -> CGFloat {
    return 2.5
}

func collectionView(_ collectionView: UICollectionView,insetForSectionAt section: int) -> UIEdgeInsets {
    return UIEdgeInsets(top: 5,left: 5,bottom: 5,right: 5)
}

func collectionView(_ collectionView: UICollectionView,minimumInteritemSpacingForSectionAt section: int) -> CGFloat {
    return 0
}

务必保持照片权限.如果单击“不允许”,则必须使用PHPhotoLibrary.authorizationStatus()管理授权.

您可以阅读有关Photos框架的更多信息.

@H_489_2@ @H_489_2@
@H_489_2@
@H_489_2@@H_489_2@

大佬总结

以上是大佬教程为你收集整理的ios – Swift 3:在不使用UIImagePickerController的情况下从照片/相机胶卷加载照片全部内容,希望文章能够帮你解决ios – Swift 3:在不使用UIImagePickerController的情况下从照片/相机胶卷加载照片所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。