gpt4 book ai didi

swift - 将相册中的照片上传到 UICollectionView

转载 作者:行者123 更新时间:2023-11-30 12:41:46 25 4
gpt4 key购买 nike

我开始学习 Swift,目前正在参加为 ios 构建应用程序的类(class)。我尝试获取照片并将它们放在 CollectionView 上,它在 Xcode 模拟器上运行良好。然而,我发现当我在 iPhone 上尝试时,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")
})

}

不知何故,在iPhone中,它仍然会请求照片,但不会显示collectionView。我对编码非常陌生,我不确定这是否合适。我们将非常高兴,并欢迎任何真诚的建议。

最佳答案

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
}

关于swift - 将相册中的照片上传到 UICollectionView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42155658/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com