Vinod Vinod - 1 month ago 17
Swift Question

Fusuma Album View video thumbs swaping

I am using Fusuma for my image and video gallery.
I have an issue with scrolling gallery View (FSAlbumView.swift) . while I scroll it swaps thumbs of video , or in place of a video thumb an image thumb come, It do not have any issues with thumbs of images, the problem is only with video thumbs.

Here is my code.

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

let cell = collectionView.dequeueReusableCellWithReuseIdentifier("FSAlbumViewCell", forIndexPath: indexPath) as! FSAlbumViewCell

let currentTag = cell.tag + 1
cell.tag = currentTag

let asset = self.images[indexPath.item] as! PHAsset
if(asset.mediaType == .Image){
self.imageManager?.requestImageForAsset(asset,
targetSize: cellSize,
contentMode: .AspectFill,
options: nil) {
result, info in

if cell.tag == currentTag {
cell.image = result
}

}
}else if(asset.mediaType == .Video){
self.imageManager?.requestAVAssetForVideo(asset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in
//dispatch_async(dispatch_get_main_queue(), {

let asset = asset as? AVURLAsset
if asset?.URL.absoluteString != nil{
self.videoURLArray.append([indexPath.row:(asset?.URL.absoluteString)!])
}
if cell.tag == currentTag {
cell.video = asset?.URL
// self.videoURLArray[indexPath.row] = (asset?.URL.absoluteString)!
print(asset?.URL)

}
//..var data = NSData(contentsOfURL: asset!.URL)

//})
})
}
return cell
}

Answer

We also had the same issue. What we did is giving below. Don't know whether its the right way.

Comment the below statements.

let currentTag = cell.tag + 1  
cell.tag = currentTag

add replace the if-else block with the following code snippet

self.imageManager?.requestImageForAsset(asset, targetSize: cellSize, contentMode: .AspectFill, options: nil){
                                                    result, info in


                                                    if(asset.mediaType == .Video){
                                                        self.imageManager?.requestAVAssetForVideo(asset, options: nil, resultHandler: {(asset: AVAsset?, audioMix: AVAudioMix?, info: [NSObject : AnyObject]?) in

                                                            let asset = asset as? AVURLAsset
                                                            if asset?.URL.absoluteString != nil{
                                                                self.videoURLArray.append([indexPath.row:(asset?.URL.absoluteString)!])
                                                            }
                                                        })
                                                    }
                                                    cell.image = result
 }
Comments