0

I am trying to retrieve an image saved using:

let pngData = UIImagePNGRepresentation(image)

let finalPath = filePath() // This method just create the path to save to.

saveImage(pngData, filePath: finalPath)

Later I want to retrieve this data and set the UIImageView of a UIButton. However when I use the following code, it just displays a completely blue image.

Here is the code:

let filePath = tempCard?.frontPhoto

let imgData = UIImage(contentsOfFile: filePath!)frontPhotoButton.setImage(imgData, forState: .Normal)

frontPhotoButton.setImage(imgData, forState: .Normal)

I am not sure why this is just showing a blue button.

Edit:

I have also tried:

 let filePath = tempCard?.frontPhoto

 let imageData = NSData(contentsOfFile: filePath!

 let image = UIImage(data: imageData!)

 frontPhotoButton.setImage(image, forState: .Normal)

Same result.

pls
  • 1,522
  • 2
  • 21
  • 41

2 Answers2

0

For anyone else who has a problem in the future setting the background image of a UIButton. Both of the above code snippets work to retrieve the image.

The Problem was, in interface builder my button was: Type - System. When I changed Type - Custom everything worked fine.

enter image description here

pls
  • 1,522
  • 2
  • 21
  • 41
0

Use this method to retrieve image from document directory of an app

func loadImageFromPath() {

    dispatch_async(dispatch_get_main_queue(), {
        let fileManager = NSFileManager.defaultManager()

        var paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0] as String
        var getImagePath = paths.stringByAppendingPathComponent("Image2.png")



        if (fileManager.fileExistsAtPath(getImagePath))
        {
            println("FILE AVAILABLE AT \(paths)");

            //Pick Image and Use accordingly
            var imageis: UIImage = UIImage(contentsOfFile: getImagePath)!

            let data: NSData = UIImagePNGRepresentation(imageis)

            self.imgProfileView.image = UIImage(data: data)
             self.imgProfileView.contentMode = UIViewContentMode.ScaleAspectFit

        }
        else
        {
            println("FILE NOT AVAILABLE");

        }

    });


}
poojathorat
  • 1,200
  • 2
  • 9
  • 19
  • It turns out my code was working, it was a the fact I had not changed the UIButton in interface builder to custom. Thanks. – pls Mar 24 '15 at 13:15