2017-02-10 65 views
0

我开始学习斯威夫特,目前就读于类,这是构建适用于iOS的应用程序。我试图获取的照片,并把它们在的CollectionView和它的的Xcode模拟器的罚款。然而,我发现,当我试图在iPhonecollectionView甚至不加载。请求图像的代码就像这样。与上传照片的相册工作的UICollectionView

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。我对编码很陌生,我不确定这是否合适。这将非常令人高兴,任何真诚的建议都会受到欢迎。

+0

而发生了什么?你是否打印(“成功”)?我期望如果它确实需要用'.reloadData()'重新加载集合视图' – Grimxn

+0

你是否认为它的打印效果非常好?当有更多图片时,他们上传时间过长。我尝试过,但似乎没有工作。 – Chajunhyung

回答

0

VAR图片: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 
}