1

Getting this error while saving image to photos: * ERROR * CGImageSource was created with data size: 22467 - current size is only: 6139

let canvasframe = self.canvasView.frame
                self.canvasView.frame = CGRectMake(0, 0, self.size.width, self.size.height)
                UIImageWriteToSavedPhotosAlbum(self.getUIImageFromThisUIView(self.canvasView),nil,nil,nil);
                self.canvasView.frame = canvasframe


func getUIImageFromThisUIView(aUIView: UIView) -> UIImage {
        UIGraphicsBeginImageContext(aUIView.bounds.size)
        aUIView.layer.renderInContext(UIGraphicsGetCurrentContext()!)
        let viewImage: UIImage = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return viewImage
    }
Mughees
  • 607
  • 1
  • 6
  • 15

2 Answers2

1

This problem is occurs when we trying to get png file from documents directory and we directly assign the content of file to local UIImage variable. Instead of doing it, First get data of content of file and then transform the data into UIImage. In short

Change this:

let img = UIImage(contentsOfFile: filename)

To This:

let img = NSData(contentsOfFile: filename)
let photo = UIImage(data: img!)!

This works for me. Hope this helps you

Mughees
  • 607
  • 1
  • 6
  • 15
0

I had same problem when downloading an image. I was presuming the file I have opening was a PNG but api was throwing a 500 and tmp/ NDData actually contained html error.

heres my answer in other SO on how to catch the bytesize

Community
  • 1
  • 1
brian.clear
  • 5,277
  • 2
  • 41
  • 62