Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/104.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在UICollectionView中对照片库中的图像进行分页_Ios_Swift_Uicollectionview - Fatal编程技术网

Ios 如何在UICollectionView中对照片库中的图像进行分页

Ios 如何在UICollectionView中对照片库中的图像进行分页,ios,swift,uicollectionview,Ios,Swift,Uicollectionview,我已经在UICollectionView中从photoLibrary导入了图像,collectionView在collectionView中初始化照片需要花费大量时间,因此我需要通过垂直滚动对这些图像进行分页。首先,我想在collectionView中加载10个图像,然后用户对加载10个图像进行分页,然后继续此过程 import UIKit import Photos import XLPagerTabStrip private let reuseIdentifier = "Cell" cl

我已经在UICollectionView中从photoLibrary导入了图像,collectionView在collectionView中初始化照片需要花费大量时间,因此我需要通过垂直滚动对这些图像进行分页。首先,我想在collectionView中加载10个图像,然后用户对加载10个图像进行分页,然后继续此过程

import UIKit
import Photos
import XLPagerTabStrip

private let reuseIdentifier = "Cell"

class PhotosRecyclerCollection: UICollectionViewController, UICollectionViewDelegateFlowLayout, UINavigationControllerDelegate {

    var imageArray = [UIImage]()

    override func viewDidLoad() {
        grabPhotos()
    }

    func grabPhotos(){

        let imgManager = PHImageManager.default()

        let requestOptions = PHImageRequestOptions()
        requestOptions.isSynchronous = true
        requestOptions.deliveryMode = .highQualityFormat

        let fetchOptions = PHFetchOptions()
        fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: false)]

        if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {

            if fetchResult.count > 0 {

                for i in 0..<fetchResult.count{

                    imgManager.requestImage(for: fetchResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {

                        image , error in

                        self.imageArray.append(image!)
                    })
                }
            }

            else {

                print("You got no photos")

                self.collectionView?.reloadData()
            }
        }

    }





    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return imageArray.count
    }

    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell

        let imageView = cell.viewWithTag(1) as! UIImageView

        imageView.image = imageArray[indexPath.row]

        return cell
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {

        let width = collectionView.frame.width / 4 - 1


        return CGSize(width: width, height: width)



    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        return 1.0
    }

    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let mainStoryBoard:UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
        let desVC = mainStoryBoard.instantiateViewController(withIdentifier: "imageCropper") as! ImageCropperViewController
        desVC.image = imageArray[indexPath.row]
        self.navigationController?.pushViewController(desVC, animated: true)
    }

}
导入UIKit
导入照片
导入XLPagerTabStrip
private let reuseIdentifier=“Cell”
类PhotoRecyclerCollection:UICollectionViewController、UICollectionViewDelegateFlowLayout、UINavigationControllerDelegate{
var imageArray=[UIImage]()
重写func viewDidLoad(){
抓取照片()
}
func grabPhotos(){
让imgManager=PHImageManager.default()
让requestOptions=PHImageRequestOptions()
requestOptions.isSynchronous=true
requestOptions.deliveryMode=.highQualityFormat
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:false)]
如果让fetchResult:PHFetchResult=PHAsset.fetchAssets(带:。图像,选项:fetchOptions){
如果fetchResult.count>0{
对于0..Int中的i{
返回imageArray.count
}
重写func collectionView(collectionView:UICollectionView,cellForItemAt indexPath:indexPath)->UICollectionViewCell{
让cell=collectionView.dequeueReusableCell(带有reuseidentifier:“photocollectionviewcell”,for:indexath)作为!photocollectionviewcell
让imageView=cell.viewWithTag(1)为!UIImageView
imageView.image=imageArray[indexPath.row]
返回单元
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,sizeForItemAt indexPath:indexPath)->CGSize{
let width=collectionView.frame.width/4-1
返回CGSize(宽度:宽度,高度:宽度)
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,MinimumLineSpacingforSection:Int)->CGFloat{
返回1.0
}
func collectionView(collectionView:UICollectionView,布局collectionViewLayout:UICollectionViewLayout,MinimumItemSpacingForSection:Int)->CGFloat{
返回1.0
}
重写func collectionView(collectionView:UICollectionView,didSelectItemAt indexPath:indexPath){
let mainStoryBoard:UIStoryboard=UIStoryboard(名称:“Main”,bundle:nil)
让desVC=mainStoryBoard.instanceeviewcontroller(标识符为:“imageCropper”)作为!ImageCropperServicewcontroller
desVC.image=imageArray[indexPath.row]
self.navigationController?.pushViewController(desVC,动画:true)
}
}

您不需要分页。因为您正在预加载所有图像,所以加载需要花费大量时间

只需停止在内存中预加载图像,并在实际需要时获取相关图像即可

为此,添加
fetchResult
作为实例变量:

private var fetchResult: PHFetchResult?
替换

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {

            if fetchResult.count > 0 {

                for i in 0..<fetchResult.count{

                    imgManager.requestImage(for: fetchResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {

                        image , error in

                        self.imageArray.append(image!)
                    })
                }
            }
返回imageArray.count
替换为
返回self.fetchResult?.count±0

最后:

let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]


您不需要分页。因为您正在预加载所有图像,所以加载需要很多时间

只需停止在内存中预加载图像,并在实际需要时获取相关图像即可

为此,添加
fetchResult
作为实例变量:

private var fetchResult: PHFetchResult?
替换

if let fetchResult : PHFetchResult = PHAsset.fetchAssets(with: .image, options: fetchOptions) {

            if fetchResult.count > 0 {

                for i in 0..<fetchResult.count{

                    imgManager.requestImage(for: fetchResult.object(at: i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .aspectFill, options: requestOptions, resultHandler: {

                        image , error in

                        self.imageArray.append(image!)
                    })
                }
            }
返回imageArray.count
替换为
返回self.fetchResult?.count±0

最后:

let imageView = cell.viewWithTag(1) as! UIImageView
imageView.image = imageArray[indexPath.row]


当我滚动collectionView时,我的图像在几毫秒后刷新,首先显示随机图像,然后更改为实际图像。当我滚动collectionView时,我的图像在几毫秒后刷新,首先显示随机图像,然后更改为实际图像。