2016-09-23 49 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来对变化做出反应在获取结果,包括动画集合视图更新
  • 处理异步取和集合视图的数据源
  • 在照片库中
插入新的资产之间的相互作用