如何异步加载CollectionView单元格图像?

时间:2016-03-03 12:23:43

标签: ios swift asynchronous uiimage uicollectionview

现在我有UICollectionView(水平滚动),其中的单元格包含UIImage。我正在使用它来显示Instagram的UIImage过滤器。但是,当我滚动查看实现过滤器后图像的外观时 - 它会冻结。我怎样才能让它变得流畅,就像在Instagram中一样?

我的代码:

func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    let cell: imageFilterCell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! imageFilterCell

    NSOperationQueue.mainQueue().addOperationWithBlock {             
        cell.imageView.image = self.applyFilterTo(self.image!, filter: self.filtersImages[indexPath.row])
        cell.imageView.layer.cornerRadius = 5
        cell.imageView.layer.masksToBounds = true
        cell.filterLabel.text = self.filtersLabels[indexPath.row]
    }

    return cell
}

其中applyFilter()实现了对我的图片的过滤。

任何使滚动顺畅并防止冻结的解决方案?

2 个答案:

答案 0 :(得分:2)

试试这个

 <input id="transportOptions" list="transport">

<datalist id="transport">
  <option value="Car">
  <option value="Bike">
  <option value="Scooter">
  <option value="Taxi">
  <option value="Bus">
</datalist> 

答案 1 :(得分:0)

试试这个

byte[] zipByteArray = null;
        int i = 0;

        if (zipFiles != null && zipFiles.Count > 0)
        {
            MemoryStream outputMemoryStream = new MemoryStream();
            ZipOutputStream zipStream = new ZipOutputStream(outputMemoryStream);
            zipStream.SetLevel(3);

            foreach (byte[] internalZipFile in zipFiles)
            {
                MemoryStream inputMemoryStream = new MemoryStream(internalZipFile);

                    ZipEntry newZipEntry = new ZipEntry("AdManifest-" + i.ToString() + ".zip");
                    newZipEntry.DateTime = DateTime.Now;
                    newZipEntry.Size = internalZipFile.Length;

                    zipStream.PutNextEntry(newZipEntry);

                    StreamUtils.Copy(inputMemoryStream, zipStream, new byte[1024]);
                    zipStream.CloseEntry();
                i++;
            }

            zipStream.IsStreamOwner = false; // to stop the close and underlying stream
            zipStream.Close();

            outputMemoryStream.Position = 0;
            zipByteArray = outputMemoryStream.ToArray();

            using (FileStream fileStream = new FileStream(@"c:\manifest.zip", FileMode.Create))
            {
                fileStream.Write(zipByteArray, 0, zipByteArray.Length);
            }
        }