2017-08-17 34 views
0

我一直試圖在調整大小後在uicollectionview中加載圖像。如何使圖像在後臺線程上調整大小,然後在uicollectionview中加載。我嘗試了下面的代碼,但圖像不加載在collectionview中。無法在全局線程中調整大小後在uicollectionview中加載圖像

功能調整

func resizeImage(image: UIImage, targetSize: CGSize) -> UIImage { 
    let size = image.size 

    let widthRatio = targetSize.width/image.size.width 
    let heightRatio = targetSize.height/image.size.height 

    // Figure out what our orientation is, and use that to form the rectangle 
    var newSize: CGSize 
    if(widthRatio > heightRatio) { 
     newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio) 
    } else { 
     newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio) 
    } 

    // This is the rect that we've calculated out and this is what is actually used below 
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height) 

    // Actually do the resizing to the rect using the ImageContext stuff 

    var newImage = UIImage() 
    DispatchQueue.global().async { 
     UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 
     image.draw(in: rect) 
     newImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 
    } 


    return newImage 
} 

這是cellForItemAt功能:

if let imageView = cell.viewWithTag(499) as? UIImageView { 
     let image = self.cardImages[indexPath.item] 
     imageView.image = resizeImage(image: image, targetSize: CGSize(width: 100, height: 100)) 

    } 

回答

0

在您使用線程的調整大小圖像的方法。所以你需要在隊列中使用一個completionHandler塊。在你的代碼中,當你的全局隊列調用異步時,你將返回一個空的圖像。如果您需要的代碼示例,讓我知道,我將編輯我的答案

=========編輯==============

func resizeImage(image: UIImage, targetSize: CGSize, completionHandler: @escaping (_ image: UIImage?) ->()) { 
    let size = image.size 

    let widthRatio = targetSize.width/image.size.width 
    let heightRatio = targetSize.height/image.size.height 

    // Figure out what our orientation is, and use that to form the rectangle 
    var newSize: CGSize 
    if(widthRatio > heightRatio) { 
     newSize = CGSize(width: size.width * heightRatio, height: size.height * heightRatio) 
    } else { 
     newSize = CGSize(width: size.width * widthRatio, height: size.height * widthRatio) 
    } 

    // This is the rect that we've calculated out and this is what is actually used below 
    let rect = CGRect(x: 0, y: 0, width: newSize.width, height: newSize.height) 

    // Actually do the resizing to the rect using the ImageContext stuff 

    var newImage = UIImage() 
    DispatchQueue.global().async { 
     UIGraphicsBeginImageContextWithOptions(newSize, false, 0.0) 
     image.draw(in: rect) 
     newImage = UIGraphicsGetImageFromCurrentImageContext()! 
     UIGraphicsEndImageContext() 

     //now we need to return to the main thread 
     //NOTE! This is Swift 3 code 
     DispatchQueue.main.async { 
      //return the image 
      completionHandler(newImage) 
     } 
    } 
} 

現在,在你想獲得的圖像的方法:

if let imageView = cell.viewWithTag(499) as? UIImageView { 
     let image = self.cardImages[indexPath.item] 

     resizeImage(image: image, targetSize: CGSize(width: 100, height: 100)) { image in 
      imageView.image = image 
     } 
} 

順便說一句,我也不會用在cellForRow方法這個方法,我更願意將其設置爲自定義單元格類。但取決於你。

+0

好的,檢查代碼,讓我知道如果你需要的東西 – Woof

+0

它的工作完美無瑕。非常感謝。 – N4SK