2015-05-11 43 views
0

我正在使用tableViews。除了順利滾動tableview之外,一切都很完美。以下是一個代碼:如何獲得平滑滾動tableView? (使用swift)

CommonFunctions.loadImageAsyncAndVignette(link, imageView: cell.imageViewMiddleSize, article: arrayMainPage[indexPath.row], viewContr: self) 

此函數異步加載圖像並使用CIFilter對其進行格式化。

class func loadImageAsyncAndVignette(stringURL: String, imageView: UIImageView, placeholder: UIImage! = nil, article: ArticleInfo, viewContr: UIViewController) { 

    imageView.image = placeholder 

    let url = NSURL(string: stringURL) 
    let requestedURL = NSURLRequest(URL: url!) 

    NSURLConnection.sendAsynchronousRequest(requestedURL, queue: NSOperationQueue.mainQueue()) { 
     response, data, error in 

     if error == nil { 

      if data != nil { 
       if let image = UIImage(data: data){ 

        //imageView.image = UIImage(data: data) 

        let string = stringURL as String 
        article.updateDictionary(stringURL, image: image) 

        let qos = Int(QOS_CLASS_USER_INTERACTIVE.value) 

        dispatch_async(dispatch_get_global_queue(qos, 0), {() -> Void in 

        let imageEdited = CommonFunctions.vignettePhoto(image, imageViewLocal: imageView) 

          dispatch_async(dispatch_get_main_queue(), { 
          imageView.image = imageEdited 
          }) 
        }) 

       } 
       else{ 
        println("image is nil") 
       } 
      } 
     }else{ 
      let stringError = error.localizedDescription 
      CommonFunctions.showAlert("Ошибка", alertText: stringError, alertButtonText: "Закрыть", viewController: viewContr) 
     } 
    } 
} 

在函數loadImageAsyncAndVigette中,我加載圖像並對其進行格式化。下面是格式化功能:

class func vignettePhoto(inputImage:UIImage, imageViewLocal: UIImageView)->UIImage{ 


     var beginImage = CIImage(image: inputImage) 
     var vignette = CIFilter(name:"CIVignetteEffect") 
     vignette.setValue(beginImage, forKey:kCIInputImageKey) 

     let centerx = (imageViewLocal.frame.width)/2 


     let centery = (imageViewLocal.frame.height)*2 
     let center = CIVector(x: centerx, y: centery) 

     vignette.setValue(center, forKey:"inputCenter") 

     vignette.setValue(0.5, forKey:"inputIntensity") 

     var radius = imageViewLocal.frame.height 
     vignette.setValue(radius, forKey:"inputRadius") 

     let newImage = UIImage(CIImage: vignette.outputImage) 

    return newImage! 
} 

我使用功能loadImageAsync在

override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell { 
commonFunction.loadImageAsync.... 

} 一切正常。結果是我想要的。但我沒有順利滾動表格視圖。在模擬器上,很難滾動。在設備上可以滾動,但無論如何,我可以看到它有點跳躍(不光滑)。那麼,有人能告訴我爲什麼滾動不順暢嗎?我使用異步加載,但它沒有幫助。

+0

你使用可重複使用的單元嗎? –

+0

你好,我不明白。你的意思是說可重複使用的細胞。我很確定這個問題是關於圖像的過濾。如果我評論過濾部分滾動順利! –

+1

儘管您正在調度到全局隊列,但請注意,此隊列的線程數可能會有限。我相信這個限制是64。因此,如果您與其他任務(例如系統任務)共享此隊列並根據您要轉換的圖像數量,您可能會填充該隊列,由於該隊列已共享,因此可能會暫停其他任務。最好使用dispatch_queue_create爲你的類創建一個隊列。在你的CommonFunctions類中。這樣,你可以控制自己的線程,並保證你沒有填滿共享隊列。 – user1171911

回答

0

爲您的代碼

可能的解決它不清楚究竟爲什麼要插入這麼多的東西進入你的圖像轉換功能,因爲目標(至少對我來說)似乎是將圖像轉換爲一個程式化的照片。如果只需要轉換原始圖像並返回轉換後的圖像,IMO功能將更加清晰。相反,你的功能是產生一個網絡請求,然後轉換圖像,並將視圖控制器作爲參數。隨着所有這些雜耍,似乎它可能會出現問題,但我的意見。

在您的常見功能小插曲照片我相信這是最好的做法,將此函數作爲封閉返回。如果這是一個計算密集型任務(基於你的問題,聽起來就像這樣),你應該把它看作是一個網絡請求,這意味着你應該在它自己的託管線程背景中或者作爲閉包來處理它。

例如:

CommonFunctions.vignettePhoto(image:UIImage, imageViewLocal:UIImageView, completion:(imageView:UIImage?)->Void) { 
    ... your conversion code ... 
    completion(imageView: theConvertedImage) 
} 

有一件事我不明白爲什麼你正在做的是插入的UIImageView在這個函數中的參數,因爲你是返回一個UIImage,而不是一個UIImageView。這似乎是多餘的,但我看不到轉換的完整代碼,所以我無法確定它是否實際上不是有用的參數。

然後,當你去顯示單元例如:

無需產生一個線程,因爲它是在一個封閉...

CommonFunctions.vignettePhoto(image, imageView) { (theConvertedImage) 
    dispatch_async(dispatch_get_main_queue(),) { 
    if let cellToUpdate = tableView.cellForIndex(indexPath) as? YourReusableTableCellClass { 
     imageView.image = theConvertedImage 
    } 
    } 
} 

但是,這僅僅是一個建議。嘗試一下,看看它是否適合你。我在我的應用程序中多次使用這種方法,並且獲得了流暢的結果。你也可能想要做的一件事是緩存圖像轉換的結果。