将相册中的照片上传到 UICollectionView

Posted

技术标签:

【中文标题】将相册中的照片上传到 UICollectionView【英文标题】:Working with an uploading Photos in Photo album to the UICollectionView 【发布时间】:2017-02-10 09:19:54 【问题描述】:

我开始学习 Swift,目前正在学习为 ios 构建应用程序的课程。我试图获取照片并将它们放在 collectionview 上,它在 Xcode 的模拟器上运行良好。但是,当我在 iPhone 上尝试时,我发现collectionView 甚至无法加载。请求图片的代码是这样的。

    let imgManager = PHImageManager.default()

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

请求选项:

    let fetchOptions = PHFetchOptions()
    fetchOptions.sortDescriptors = [NSSortDescriptor(key: "creationDate", ascending: true)]
    if let fetchResult :PHFetchResult = PHAsset.fetchAssets(with: .image , options: fetchOptions) 

        if fetchResult.countOfAssets(with: PHAssetMediaType.image) > 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!)
                self.PHArray.append(fetchResult.object(at: i))
                    print("Success")
                )

            

不知何故,在 iPhone 中它仍然会请求照片,但不会显示 collectionView。我对编码很陌生,我不确定这是否合适。非常高兴,欢迎任何真诚的建议。

【问题讨论】:

发生了什么?你print("Success") 着火了吗?我希望如果确实如此,您需要使用 .reloadData() 重新加载集合视图 您是否打印得非常好?当有更多图片时,它们需要很长时间才能上传。我试过了,但它似乎不起作用。 【参考方案1】:

var images:NSMutableArray = NSMutableArray()

func fetchPhotos ()

    images = NSMutableArray()
  //  totalImageCountNeeded = 3
    self.fetchPhotoAtIndexFromEnd(0)


func fetchPhotoAtIndexFromEnd(index:Int)

    let status : PHAuthorizationStatus = phphotoLibrary.authorizationStatus()
    if status == PHAuthorizationStatus.Authorized
    
    let imgManager = PHImageManager.defaultManager()
    let requestOptions = PHImageRequestOptions()
    requestOptions.synchronous = true

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

    if let fetchResult: PHFetchResult = PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image, options: fetchOptions)
    
        if fetchResult.count > 0
        
            imgManager.requestImageForAsset(fetchResult.objectAtIndex(fetchResult.count - 1 - index) as! PHAsset, targetSize: CGSizeMake(self.img_CollectionL.frame.size.height/3, self.img_CollectionL.frame.size.width/3), contentMode: PHImageContentMode.AspectFill, options: requestOptions, resultHandler:  (image, _) in
                //self.images.addObject(image!)
                if image != nil
                
                    self.images.addObject(image!)
                
                if index + 1 < fetchResult.count && self.images.count < 20 //self.totalImageCountNeeded
                
                    self.fetchPhotoAtIndexFromEnd(index + 1)
                
                else
                
                    print("Completed array: \(self.images)")
                
            )
            self.img_Collection.reloadData()
        
    
    


func collectionView(_ collectionView: UICollectionView,
                           numberOfItemsInSection section: Int) -> Int 

   return images.count


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

   let cell  = collectionView.dequeueReusableCellWithReuseIdentifier("cell1", forIndexPath: indexPath)
   let imagess_ : UIImageView = cell.viewWithTag(100) as! UIImageView
   imagess_.image = images [indexPath.row] as? UIImage
        return cell

【讨论】:

以上是关于将相册中的照片上传到 UICollectionView的主要内容,如果未能解决你的问题,请参考以下文章

苹果手机怎么上传照片到qq空间

苹果13相册上传啥意思

通过 Graph API 和 iOS 将照片上传到相册时出现 Facebook 错误:(#1) 发生未知错误

如何向网站上传图片?

html5 如何调用手机相册,进行上传照片

苹果手机QQ空间怎么上传照片?