当我打开我的应用程序时,它会要求我允许访问我的照片。一切都好。但是在接受之后,屏幕变黑并且没有显示照片,在我的控制台中它说没有找到照片。虽然我的设备上有照片。
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 :(得分:3)
这里的问题很可能是你只要求资产一次。你第一次问,你什么也得不到。当您致电PHAsset.fetchAssetsWithMediaType
时,它会同时执行两项 :它会要求用户授权,并且因为授权尚未授权(授权提示仍处于启用状态)屏幕),它返回一个空的获取结果。 (因为获取结果为空,你不能
如果出现问题,您应该在第二次运行应用时看到非空的获取结果(因为授权是一次性的,并且会在启动时保持不变)。
有几种方法可以解决这个问题......
只需先使用requestAuthorization()
直接请求授权,然后仅在该呼叫的完成处理程序触发后获取资产(结果为正)。
在执行提取之前,另一个是register()
您的视图控制器(或您的其他控制器类)作为Photos库的观察者。您在授权之前首次提取仍然会返回空结果,但只要用户授权您的应用,您的观察者就会收到photoLibraryDidChange()
来电,您可以通过该电话访问完整的&#34;真实&#34;获取结果。
除此之外,还有其他一些你在做的不是最好的做法...
无论用户是否愿意看到图片库中的每个资源,您都会从PHImageManager
请求图片。考虑一个人的情况,其中iCloud照片库包含数以万计的资产 - 只有前30个左右的资产一次适合您的集合视图,并且用户可能永远不会一直滚动到最后,但是你&#39 ;仍然为每张图片获取缩略图。 (如果iCloud资产不在本地设备上,则会触发用户可能永远看不到的大量缩略图的网络活动。)
您同步并按顺序从PHImageManager
获取缩略图。一旦得到处理的授权问题,您的应用程序可能会崩溃,因为您的for i in 0..<fetchResult.count { imgManager.requestImageForAsset
循环会阻塞主线程太长时间。
Apple提供了canonical example code,演示如何使用照片应用来执行显示完整缩略图的集合视图等操作。看看该项目中的AssetGridViewController.swift
,看看他们正在做的一些事情:
PHCachingImageManager
根据集合视图的可见区域预取批量缩略图PHPhotoLibraryObserver
对获取结果中的更改做出反应,包括动画集合视图更新答案 1 :(得分: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 输入标识符
2.3 输入课程
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
}
答案 2 :(得分:-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
}