2016-06-03 4 views
0

저는 UICollectionView (가로)를 가지고 있으며 셀에 적용된 필터가있는 이미지를 넣었습니다.필터링 된 이미지를 배열에 저장하는 방법은 무엇입니까?

나는이 한 :

var filtered = [Int: UIImage]() 
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { 
    let cell = collectionView.dequeueReusableCellWithReuseIdentifier("filterCell", forIndexPath: indexPath) as! filterCell 

    let op1 = NSBlockOperation {() -> Void in 
     let img = self.image! 
     let img1 = self.applyFilterTo(img, filter: self.filtersImages[indexPath.row]) 

     NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in 

      if let filtered = self.filtered[indexPath.row] { 
       cell.imageView.image = self.filtered[indexPath.row] 
      } else { 
       self.filtered[indexPath.row] = img1 
       cell.imageView.image = self.filtered[indexPath.row] 

      } 
     }) 
    } 

    self.queue!.addOperation(op1); 

    return cell 
} 

을 여기서

var myFilter = CIFilter() 
func applyFilterTo(image: UIImage, filter: String) -> UIImage { 
    let sourceImage = CIImage(image: image) 

    myFilter = CIFilter(name: filter)! 
    myFilter.setDefaults() 

    myFilter.setValue(sourceImage, forKey: kCIInputImageKey) 

    let context = CIContext(options: nil) 

    let outputCGImage = context.createCGImage(myFilter.outputImage!, fromRect: myFilter.outputImage!.extent) 

    let newImage = UIImage(CGImage: outputCGImage, scale: image.scale, orientation: image.imageOrientation) 

    return newImage 
} 

그래서 여기에 원칙은 내 사전에 저장하고 나중에 사전에서 스크롤 부하 이미지에 이미지에 내 필터를 적용한다 . 그러나 여전히 이미지를 사용하고 필터를 적용한 다음 나중에 보여줍니다. 그래서 스크롤 내 UICollectionView 고정, 이미지에 필터를 변경합니다.

내가 뭘 잘못하고 어떻게 해결할 수 있습니까?

답변

0

이미지가 self.filtered[indexPath.row] 인 경우 let img1 = self.applyFilterTo(img, filter: self.filtersImages[indexPath.row])해야하는 이유는 무엇입니까?

그래서 나는 if let filtered = self.filtered[indexPath.row] {을 점검하고 그렇지 않다면 그냥 필터를 시작한다고 생각합니다.

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

    if let filtered = self.filtered[indexPath.row] { 
     cell.imageView.image = filtered 
    } else { 
     let op1 = NSBlockOperation {() -> Void in 
     let img = self.image! 
     let img1 = self.applyFilterTo(img, filter: self.filtersImages[indexPath.row]) 

     NSOperationQueue.mainQueue().addOperationWithBlock({() -> Void in 
      self.filtered[indexPath.row] = img1 
      cell.imageView.image = self.filtered[indexPath.row] 
     }) 
     } 

     self.queue!.addOperation(op1); 
    } 

    return cell 
} 
: 코드에 대한

는이 같은 것

관련 문제