The best way to handle image in tableviewcell [swift]

Viewed 422

I've table view cell that require to show 10-30 image with high resolution (for print purposes). The app got memory warning and crash when we had 15 photos or so. How the best way to handle photo in tableview cell so the app wont crash?

Class Data

class dataImage {
    var userId: String
    var value: Double
    var photo: UIImage?
    var croppedPhoto: UIImage?

init(userId:String, value: Double, photo: UIImage?, croppedPhoto: UIImage?){
    self.userId = userId
    self.value = value
    self.photo = photo
    self.croppedPhoto = croppedPhoto
}

}

View Controller

func loadDataPhoto(){
        for asset in photos{
            autoreleasepool{
            asset.fetchOriginalImageWithCompleteBlock({ image, info in
                let images = image
                let croppedImage: UIImage = self.photoToCrop(image: image!)
                let data1 = dataImage(userId: "goodies\(completed+1)", value: 1.0, photo: images, croppedPhoto: croppedImage)
                datas += [data1]
                print("*")
                //print(data1.userId)
                self.tableView.reloadData()

            })
        }


    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
    let cellIdentifier = "reviewCell"
    let cell = tableView.dequeueReusableCellWithIdentifier(cellIdentifier, forIndexPath: indexPath) as! ReviewTableViewCell

    let data = datas[indexPath.row]
    let tag = indexPath.row
    cell.tag = tag
    autoreleasepool { () -> () in
    cell.imageView.image = data.croppedPhoto
    }
    cell.countStepper.value = datas[indexPath.row].value
    cell.stepperLabel.text = "x \(Int(cell.countStepper.value))"

    cell.delegate = self
    return cell
}
0 Answers
Related