我需要在PHAsset
中组合UIImage
和UICOllectionView
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和结果显示在collectionView的UIImageView中?
downloadImage是:
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()
}
}
答案 0 :(得分:0)
在我的一个项目中,我也面临着同样的问题。需要在同一UICollectionView中处理UIImage和字符串url。您可以执行类似的操作;
// first create struct with datatypes
struct imageItem {
var stringImageURL:String
var imageGallery:UIImage
}
var array_ImagesList: [imageItem] = []
//,然后您可以根据需要在任意位置附加图像或字符串url //在UIImagePickerController代表内部,我要附加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
}