Xcode 8.3 和 Swift 3 :D在 UI 中的单元格中播放图像 从 URL 获取并存储在 NSMutable



我使用以下一段代码从 Web 服务获取数据

func ws_GetRelatedRresourcePath(_ RresourceId:Int, arrRresourceIDs:NSArray) {
let refID = arrRresourceIDs.object(at: RresourceId);
AlbumModel.getRelatedRresourcePath(parameters: refID as AnyObject?) { (responseObject,success) in
if(success){
let tempResp = responseObject;
if tempResp != nil  {
self.arrRelatedRresource.add(tempResp!)
}else {
if(responseObject != nil){
print(responseObject!);
}
}
if RresourceId < arrRresourceIDs.count - 1 {
self.ws_GetRelatedRresourcePath(RresourceId + 1, arrRresourceIDs: arrRresourceIDs);
}else{
print("Done.")
self.collectionSameSeriesMedia.reloadData();
self.setupMediaView();
AppUtils.stopLoading();
}
}

通过实现这一点,我获得了存储在名为arrRelatedRresource的NSmutableArray中的图像的URL。

我正在将此数组传递给单元格项目,如下所示

let cell = collectionSameSeriesMedia.dequeueReusableCell(withReuseIdentifier: "SameSeriesMediaIdentifier", for: indexPath) as! MediaDetailsCollectionViewCell
var strImagePath = arrRelatedRresource[indexPath.row] as! String
print(strImagePath);
strImagePath = (strImagePath as AnyObject).replacingOccurrences(of: "\" , with: "");
strImagePath = strImagePath.replacingOccurrences(of: """ , with: "");
print(strImagePath)
cell.imgSameSeriesMedia.sd_setImage(with: URL(string: strImagePath))
return cell

已编辑 :- (已解决(

问题出在存储数据的数组上。数组使用额外的">存储 URL。 我通过在中添加以下代码行解决了这个愚蠢的问题。

strImagePath = strImagePath.replacingOccurrences(of: """ , with: "");

这个问题由此解决了。

将sd_setImage与完成处理程序一起使用,并检查是否获得 nil 图像。

var strImagePath = arrRelatedRresource[indexPath.row] as! String
strImagePath = strImagePath.addingPercentEncoding(withAllowedCharacters: CharacterSet.urlQueryAllowed)!
let url = URL(string: strImagePath)
if let url = url{
NSLog("url = %@",url)
cell.imgSameSeriesMedia.sd_setImage(with: url as URL!, placeholderImage: nil, options: SDWebImageOptions(), completed: { (image: UIImage?, error: Error?, cacheType: SDImageCacheType, imageURL: URL?) in
if image != nil{
cell.imgSameSeriesMedia.backgroundcolor = UIColor.green
}
else{
cell.imgSameSeriesMedia.backgroundcolor = UIColor.blue
}
})
}