0

I've been searching for this problem for a few days already. Still no answer. I'm trying to send multiple images to the server, so, I need to convert the uiimage to nsdata first. But the nsdata seem to be a nil, but the 1st image always successful to be convert. Here is the code:

    for image in images {

        var imageTemp: NSData?

        if let image_data = UIImageJPEGRepresentation(image, 1) {

            imageTemp = image_data
        }

        if imageTemp == nil
        {
            print("nil")
            return
        }

        i = i + 1;
    }
user3057414
  • 41
  • 1
  • 9
  • the loop execute the condition here `imageTemp = image_data` or not – Anbu.Karthik Aug 23 '16 at 05:02
  • Add an `else` branch to the `if let`, set a breakpoint in it, and then inspect the console for error messages. – kennytm Aug 23 '16 at 05:39
  • @Anbu.Karthik yup, it is. For the image[0] it is okay but for the others are not. – user3057414 Aug 23 '16 at 06:21
  • it means it takes time to convert from image to nsadata – Anbu.Karthik Aug 23 '16 at 06:22
  • @kennytm it still catch the image as nil. Why the the UIImageJPEGPresentation detect the 2nd and above image as nil? – user3057414 Aug 23 '16 at 06:31
  • @Anbu.Karthik ah really. So, how do i solve it? Any suggestion? – user3057414 Aug 23 '16 at 06:41
  • @kennytm Can i download the images for testing? I used to have this problem. You may try both `UIImageJPEGRepresentation` and `UIImagePNGRepresentation`. – jhd Aug 23 '16 at 12:03
  • @Joe If he uses UIImagePNGRepresentation he will lose the orientation information of the image. That means that if you turn an image in camera app, it will turn back after converting with UIImagePNGRepresentation. – M. Kremer Aug 23 '16 at 15:23
  • @Joe I used the default images in the simulator, idk if that cause the problem. I already tried both, same result. I picked the images using BSImagePicker, and convert the PHAsset result into an array of images. The first image work fine. Sometimes the second image also fine too, if i picked more than 2 images. So how do u solved it before? – user3057414 Aug 24 '16 at 03:47

0 Answers0