2016-09-23 47 views
0

當我打開我的應用程序時,它會詢問我是否允許訪問我的照片。一切都好。但接受後,屏幕變黑,沒有顯示任何照片,並在我的控制檯中顯示它沒有找到照片。雖然我的設備上有照片。未在收藏視圖中顯示的照片

import UIKit 
import Photos 

class ViewController: UICollectionViewController, UICollectionViewDelegateFlowLayout { 
    var imageArray = [UIImage]() 

    override func viewDidLoad() { 
     grabPhotos() 
    } 

    func grabPhotos(){ 
     let imgManager = PHImageManager.defaultManager() 

     let requestOptions = PHImageRequestOptions() 
     requestOptions.synchronous = true 
     requestOptions.deliveryMode = .HighQualityFormat 

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

     if let fetchResult : PHFetchResult = PHAsset.fetchAssetsWithMediaType(.Image, options: fetchOptions){ 
      if fetchResult.count > 0 { 
       for i in 0..<fetchResult.count{ 
        imgManager.requestImageForAsset(fetchResult.objectAtIndex(i) as! PHAsset, targetSize: CGSize(width: 200, height: 200), contentMode: .AspectFill, options: requestOptions, resultHandler: { 

         image, error in 

         self.imageArray.append(image!) 
        }) 
       } 
      } 
      else{ 
       print("You have got not photos!") 
       self.collectionView?.reloadData() 
      } 
     } 
    } 

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

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
     let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) 

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

     imageView.image = imageArray[indexPath.row] 

     return cell 
    } 

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize { 
     let width = collectionView.frame.width/3 - 1 

     return CGSize(width: width, height: width) 
    } 

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

    func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAtIndex section: Int) -> CGFloat { 
     return 1.0 
    } 
} 
+0

我看到你在cellForItem方法中使用cell.viewWithTag(1)。你知道如何爲你的tableView創建一個單元格原型,所以你可以設計你的單元格並將它與圖像內容連接起來? – pedrouan

回答

-1

用於小區一類,把ImageView的在你的細胞,使其出口

現在寫這篇

override func collectionView(collectionView: UICollectionView,  cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("Cell", forIndexPath: indexPath) as! 'your class name for cell' 

cell.Image.image = imageArray[indexPath.row] 

return cell 



} 
0

1)子類一個UICollectionViewCell:

import ... 
//put this e.g. to the top of your swift file, right after import statements 

class PhotoViewCell: UICollectionViewCell { 

    @IBOutlet weak var myImageView: UIImageView! 

} 

2)創建原型爲你細胞:

2.1設計它(你可能在之前已經完成)

2.2輸入的標識符

enter image description here

2.3輸入課程

enter image description here

2.4 CTRL拖動從故事板:從細胞的的ImageView到@IBOutlet線(來自步驟1)

3)更新您的cellForItem方法

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

    cell.myImageView.image = imageArray[indexPath.row] 
    return cell 
} 
3

這裏最可能的問題是您只需要一次資產。你第一次問,你什麼也得不到。當您撥打PHAsset.fetchAssetsWithMediaType時,同時執行兩項操作::它要求用戶進行授權,並且由於授權尚未授予(授權提示仍在屏幕上),因此會返回一個空的提取結果。 (並且因爲提取結果爲空,所以您不能

如果這是問題,那麼您在第二次運行應用程序時應該會看到非空的提取結果(因爲授權是一次性操作並且持續存在啓動)。

有一些方法來解決這個問題...

  • 一個是簡單地直接申請許可首先使用requestAuthorization()後,才調用的完成處理火災獲取資產(以積極的結果)

  • 另一種方法是在執行提取操作之前將您的視圖控制器(或您的其他控制器類)作爲照片庫的觀察者。您在授權前的第一次獲取仍會返回空的結果,但只要用戶授權您的應用程序,您的觀察者就會獲得一個photoLibraryDidChange()調用,您可以從中調用完整的「真實」獲取結果。


除此之外,還有你這樣做不是在這裏的最佳實踐一些其他的東西...

PHImageManager
  • 你請求圖像的每個資產在圖書館裏,無論用戶是否會看到他們。考慮一下其中iCloud照片庫包含數以萬計資產的人的情況 - 只有前30個左右纔會立即適合您的收藏視圖,並且用戶可能永遠不會一直滾動到最後,但是您仍然可以獲取Every Image Ever的縮略圖。 (如果iCloud資產不在本地設備上,則會觸發用戶可能看不到的大量縮略圖的網絡活動。)

  • 您將從PHImageManager同步並按順序獲取縮略圖。一旦處理了授權問題,您的應用可能會崩潰,因爲您的for i in 0..<fetchResult.count { imgManager.requestImageForAsset循環會阻塞主線程時間過長。

蘋果提供canonical example code演示如何使用照片應用程序做事情像顯示收集全視縮略圖。看看AssetGridViewController.swift在該項目中看到一些東西,他們正在做的:

  • 使用PHCachingImageManager預取縮略圖的批次基於集合視圖的可見區域
  • 使用PHPhotoLibraryObserver來對變化做出反應在獲取結果,包括動畫集合視圖更新
  • 處理異步取和集合視圖的數據源
  • 在照片庫中
插入新的資產之間的相互作用