Swift 在 CollectionView 中结合 PHAsset 和 UIImage
Posted
技术标签:
【中文标题】Swift 在 CollectionView 中结合 PHAsset 和 UIImage【英文标题】:Swift combine PHAsset and UIImage in CollectionView 【发布时间】:2018-10-10 12:38:19 【问题描述】:我需要在UICOllectionView
中合并PHAsset
和UIImage
UIImage 从字符串中获取并转换为图像
从设备相册获取PHAsset
var assetCollection: PHAssetCollection!
var photosAsset: PHFetchResult<AnyObject>!
var assetThumbnailSize: CGSize!
var downloadImages: [String] = []
在 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
如何结合 downloadIm 和 result 在 collectionView 上的 UIImageView 中显示?
下载图片是:
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()
【问题讨论】:
请澄清您的问题。什么是“结合 PHAsset 和 UIImage”? @Ilya Kharabet,需要将所有来自 downloadIm(UIImage) 和所有来自 result(PHAsset) 的内容添加到 UICollectioView 中 【参考方案1】:我在我的一个项目中面临同样的问题。需要在同一个 UICollectionView 中同时处理 UIImage 和字符串 url。你可以实现类似的;
// first create struct with datatypes
struct imageItem
var stringImageURL:String
var imageGallery:UIImage
var array_ImagesList: [imageItem] = []
// 然后你可以根据功能在任何你想要的地方附加图像或字符串 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
【讨论】:
以上是关于Swift 在 CollectionView 中结合 PHAsset 和 UIImage的主要内容,如果未能解决你的问题,请参考以下文章
在 Swift 3 中使用 CGSize 的 collectionView 的 100% 宽度
Swift -collectionView 单元格内的属性在 collectionView.performBatchUpdates > reloadItems(at:) 运行后没有更新
在 Swift 中以编程方式为 CollectionView 设置插图