Swift 3:在不使用UIImagePickerController的情况下从照片/相机胶卷加载照片 [英] Swift 3: Load photos from Photos/Camera Roll without using UIImagePickerController

查看:120
本文介绍了Swift 3:在不使用UIImagePickerController的情况下从照片/相机胶卷加载照片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题之前已被提出但没有Swift 3的答案。我正在寻找过去3周内遇到的相同解决方案。

This question has been asked before but there were no answers for Swift 3. I am looking for the same solution which I am stuck for the past 3 weeks.

我做了我的研究和观看了许多Youtube视频,关于使用UIImagePickerController将图像从照片/相机胶卷加载到应用程序中,但我想在没有用户操作的情况下访问照片。

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的情况下访问这些照片?

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?

推荐答案

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

You can use the Photos framework to fetch the photos from CameraRoll/Photos.

以下是 Swift 3 代码的版本。

导入照片框架

import Photos

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

使用此功能在 viewDidLoad 中的某处或者在您想要获取照片的地方拍摄照片。

Use this function to fetch the photos, somewhere in viewDidLoad or on your action wherever you want to fetch the photos.

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 数据源方法。

Rest are UICollectionView Datasource and Delegates. See the cellForItem datasource method on How to show the image.

 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
}

确保保持照片权限<强>打开。如果单击不允许,则必须使用 PHPhotoLibrary.authorizationStatus()

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

来管理授权。您可以阅读有关照片框架的更多信息。

You can read more about Photos framework.

这篇关于Swift 3:在不使用UIImagePickerController的情况下从照片/相机胶卷加载照片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆