Swift 使用将相册中的照片上载到UICollectionView的
我开始学习Swift,目前正在参加为ios构建应用程序的课程。我试图获取照片并将其放在collectionview上,在Xcode模拟器上运行良好。然而,当我在iPhone上试用时,我发现Swift 使用将相册中的照片上载到UICollectionView的,swift,uicollectionview,photosframework,Swift,Uicollectionview,Photosframework,我开始学习Swift,目前正在参加为ios构建应用程序的课程。我试图获取照片并将其放在collectionview上,在Xcode模拟器上运行良好。然而,当我在iPhone上试用时,我发现collectionView甚至无法加载。请求图像的代码如下所示 let imgManager = PHImageManager.default() let requestOptions = PHImageRequestOptions() requestOptions.isSynchr
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")
})
}
let fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:true)]
如果让fetchResult:PHFetchResult=PHAsset.fetchAssets(带:。图像,选项:fetchOptions){
如果fetchResult.countOfAssets(带有:PHAssetMediaType.image)>0{
对于0..var图像中的i:NSMutableArray=NSMutableArray()
func fetchPhotos()
{
images=NSMutableArray()
//TotalImageCountRequired=3
self.fetchPhotoAtIndexFromEnd(0)
}
func fetchPhotoAtIndexFromEnd(索引:Int)
{
let状态:PHAuthorizationStatus=PHPhotoLibrary.authorizationStatus()
如果状态==授权状态。已授权
{
让imgManager=PHImageManager.defaultManager()
让requestOptions=PHImageRequestOptions()
requestOptions.synchronous=true
让fetchOptions=PHFetchOptions()
fetchOptions.sortDescriptors=[NSSortDescriptor(键:“creationDate”,升序:true)]
如果让fetchResult:PHFetchResult=PHAsset.fetchAssetsWithMediaType(PHAssetMediaType.Image,选项:fetchOptions)
{
如果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,选项:requestOptions,resultHandler:{(image,)in
//self.images.addObject(image!)
如果图像!=nil
{
self.images.addObject(image!)
}
如果索引+1Int
{
返回图像数
}
func collectionView(collectionView:UICollectionView,
cellForItemAt indexPath:indexPath)->UICollectionViewCell
{
let cell=collectionView.dequeueReusableCellWithReuseIdentifier(“cell1”,forIndexPath:indexPath)
让imagess_u3;:UIImageView=cell.viewWithTag(100)为!UIImageView
imagess_uxAE.image=images[indexPath.row]作为?UIImage
返回单元
}
发生了什么事?你是否打印(“成功”)
开火?我想如果是这样,你需要用.reloadData()
重新加载收藏视图,你知道它的打印是否非常大吗?当有更多图片时,它们需要花费很长时间才能上载。我尝试过,但似乎不起作用。
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
}