1

I get all the images from a photo library and I want to import it in the core data. I am trying to convert the images to NSData, in order to save it in the core data. Some images are converted, but others are not. I get the following error:

**Data[602] <Error>: ImageIO: PNG zlib error**

I use the following code:

for(_ , value) in dictOfPhoto.enumerate() {
    if let convertImage = UIImagePNGRepresentation(value.1) {
        if let app = UIApplication.sharedApplication().delegate as? AppDelegate {
            let connection = app.managedObjectContext
            let imgN = NSEntityDescription.insertNewObjectForEntityForName("Images",
                inManagedObjectContext: connection)
            imgN.setValue(convertImage, forKey: "image")
            imgN.setValue(folderName, forKey: "folderName")
            do {
                try connection.save()
                // print("Image Is Save It") 
            } catch {
                print(error)
            }
        }
    } else {
        //print("One Image Not Converted!")
    }   
}

How can I resolve this issue?

Tom Harrington
  • 69,312
  • 10
  • 146
  • 170
Hamza Almass
  • 41
  • 1
  • 6
  • How do you populate `dictOfPhoto`? – Larme May 24 '16 at 09:28
  • when i display the images from the photo library in my custom collection view i select the image from func did select and i add each image in sell i select it to dictofPhoto i have write the following code func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { if editing { if let cell = myCollectionView.cellForItemAtIndexPath(indexPath) as? ImportCollectionViewCell { dictOfPhoto[indexPath.row] = cell.imageViewCell.image } } } – Hamza Almass May 24 '16 at 09:44
  • That error message suggests that the problem is in `UIImagePNGRepresentation`, not when adding the image to Core Data. You should check what line actually produces that error message. – Tom Harrington May 24 '16 at 16:15
  • Yes i know not from the core data but my code can't convert any image to nsdata i dont know why ? The forth image in photo library is not convert to NSData but others is converted can you tell me why? – Hamza Almass May 24 '16 at 19:53
  • Not without knowing more about the images. There's no good explanation for failing one time out of four for no reason. It probably depends on the specific images in some way. – Tom Harrington May 25 '16 at 16:24

0 Answers0