swiftuicollectionviewuicollectionviewcellresize-image

Swift - cellForItemAtIndexPath unwrapping an Optional value when resizing image


i need resize dynamically an imagView when scroll a collection view in swift...

i use this function for get event of scrolling:

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {

                // get a reference to our storyboard cell
         let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyCollectionViewCell
         let indexPathItem = self.items[indexPath.item]
         if let myImage = cell.myImage{
             myImage.image = imageWithImage(UIImage(named: indexPathItem)!, scaledToSize: CGSize(width: sizeImage, height: sizeImage))
         } 

      return cell
    }

and this function for resize image:

func imageWithImage(image:UIImage,scaledToSize newSize:CGSize)->UIImage{

    UIGraphicsBeginImageContext( newSize )
    image.drawInRect(CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
    let newImage = UIGraphicsGetImageFromCurrentImageContext()
    UIGraphicsEndImageContext()
    return newImage.imageWithRenderingMode(.AlwaysTemplate)
}

debugged stops when run this line:

myImage.image = imageWithImage(UIImage(named: indexPathItem)!, scaledToSize: CGSize(width: sizeImage, height: sizeImage))

with error

"fatal error: unexpectedly found nil while unwrapping an Optional value"

working if i replace that with this:

cell.myImage.image = UIImage(named : self.items[indexPath.item])

But obviously it does not reduce the image

Can help me pls???

SOLUTION: i need resize object "cell.myImage" and not "cell.myImage.image" so solved with:

  cell.myImage.frame = CGRect(x:0.0,y:0.0,width:sizeImage,height:sizeImage) 

and set mode of imageView into cell like aspect fit.


Solution

  • You could use safer code like below to avoid crashes while unwrapping an Optional value.

    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
    
            // get a reference to our storyboard cell
            let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! YourCell
    
            cell.myImage?.image = imageWithImage(UIImage(named: self.items[indexPath.item]), scaledToSize: CGSize(width: sizeImage, height: sizeImage))
    
            return cell
        }
    
        func imageWithImage(image:UIImage?,scaledToSize newSize:CGSize)->UIImage?{
    
            guard let drawImage = image else {
    
                return nil
            }
    
            UIGraphicsBeginImageContext( newSize )
            drawImage.drawInRect(CGRect(x: 0,y: 0,width: newSize.width,height: newSize.height))
            let newImage = UIGraphicsGetImageFromCurrentImageContext()
            UIGraphicsEndImageContext()
            return newImage?.imageWithRenderingMode(.AlwaysTemplate)
        }
    

    your cellForItemAtIndexPath code is incomplete below line

        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as!
    

    so I have added YourCell for example.