简体   繁体   English

Swift 在 CollectionView 中结合 PHAsset 和 UIImage

[英]Swift combine PHAsset and UIImage in CollectionView

I need to combine PHAsset and UIImage in UICOllectionView我需要在UICOllectionView结合PHAssetUIImage

UIImage get from string and converter to image UIImage 从字符串和转换器获取图像

PHAsset get from device album PHAsset 从设备相册中获取

var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult<AnyObject>!
var assetThumbnailSize: CGSize!
var downloadImages: [String] = []

In cellForItemAt I try:在 cellForItemAt 我尝试:

    let indexP = indexPath.row

    let asset: PHAsset = self.photosAsset[indexP] as! PHAsset

    PHImageManager.default().requestImage(for: asset, targetSize: self.assetThumbnailSize, contentMode: .aspectFill, options: nil, resultHandler: {(result, info)in
    if result != nil {
    let download = self.downloadImages[indexP]
    let downloadIm = "http://...\(download)"

     cell.albumImage.downloadImage(from: downloadIm)

     cell.albumImage.image = result

}
})
return cell

how can I combine downloadIm and result to show in UIImageView on collectionView?如何将 downloadIm 和 result 结合起来显示在 collectionView 上的 UIImageView 中?

downloadImage is :下载图片是:

extension UIImageView {

    func downloadImage(from imgURL: String!) {
        let url = URLRequest(url: URL(string: imgURL)!)

        let task = URLSession.shared.dataTask(with: url) {
            (data, response, error) in

            if error != nil {
                print(error!)
                return
            }

            DispatchQueue.main.async {
                self.image = UIImage(data: data!)
            }

        }

        task.resume()
    }
}

I am facing this same problem in one of my project.我在我的一个项目中面临同样的问题。 Need to handle both UIImage and string url in same UICollectionView.需要在同一个 UICollectionView 中同时处理 UIImage 和 string url。 You can implement similar to this;你可以实现类似的;

// first create struct with datatypes
struct imageItem {
    var stringImageURL:String 
    var imageGallery:UIImage
}
var array_ImagesList: [imageItem] = []

// then you can append image or string url wherever you want as per functionality // inside UIImagePickerController Delegates I am append UIImage // 然后你可以根据功能在任何你想要的地方附加图像或字符串 url // 在 UIImagePickerController Delegates 我附加 UIImage

func imagePickerController(_ picker: UIImagePickerController, 
  didFinishPickingMediaWithInfo info: [String : Any]) {

    if let image = info[UIImagePickerControllerOriginalImage] as? UIImage
    {
        array_ImagesList.append(imageItem.init(stringImageURL: "", 
        imageGallery: image))
    }
   picker.dismiss(animated: true, completion: nil);
 }

// and in some other function I want to append image from server URL

func addImagesFromServer() {
let imageUrl: String = "htps://someserver/image.png"
 array_ImagesList.append(imageItem.init(stringImageURL: imageUrl!, 
 imageGallery:""))

 collectionView.reloadData()
}

//MARK: CollectionView DataSources & Delegates
func collectionView(_ collectionView: UICollectionView, 
   numberOfItemsInSection section: Int) -> Int {
    return array_ImagesList.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt 
    indexPath: IndexPath) -> UICollectionViewCell{
    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: 
      "kGalleryImageCell", for: indexPath as IndexPath) as! 
     AddImagesCollectionViewCell
    //  check to show image or from url here
      // for image
       cell.imageView.image = array_ImagesList[indexPath.row].imageGallery
      // from image url
                let stringUrlImage = 
                    array_ImagesList[indexPath.row].stringImageURL
                let placeholderImage = UIImage(named: 
                "logoCategorySmallIcon")!
                if(stringUrlImage.isEmpty){
                    cell.imageGalleryCell.image = placeholderImage

                }else{
                    var stringImageUrl = imageHostAPI+"\(stringUrlImage)"
                    stringImageUrl = 
             stringImageUrl.addingPercentEncoding(withAllowedCharacters: 
            CharacterSet.urlQueryAllowed)!
                    let url = URL(string: stringImageUrl)

                    cell.imageView.sd_setShowActivityIndicatorView(true)
                    cell.imageView.sd_setIndicatorStyle(.gray)
                    cell.imageView.sd_setImage(with: url!, 
                   placeholderImage: placeholderImage)
                }

          return cell
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM