0

I have image loaded from UIImagePickerController into UIImageView

I need to convert the image into string to send it to the server

This is my code but it gives an error

var imageData:String = "";
        if let unwrappedImage = profileImage.image {
            let imageNSData : NSData = UIImageJPEGRepresentation(unwrappedImage, 1.0)!;
            imageData =  (NSString(data:imageNSData, encoding:NSUTF8StringEncoding) as String?)!
        }

I am getting the following exception

fatal error: unexpectedly found nil while unwrapping an Optional value

can any one help please

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Amira Elsayed Ismail
  • 9,216
  • 30
  • 92
  • 175
  • 3
    You can't convert the image's `NSData` to an `NSString` like that since the data doesn't represent the UTF-8 encoding a string. Do some research on converting an image's data to a base64 encoded string. – rmaddy Apr 16 '16 at 00:49
  • 1
    See https://stackoverflow.com/questions/11251340/convert-uiimage-to-base64-string-in-objective-c-and-swift – rmaddy Apr 16 '16 at 00:51
  • Thanks @rmaddy your answer was very helpful. – Amira Elsayed Ismail Apr 16 '16 at 14:54

1 Answers1

0

Convert the data information of a image to a string , you will get a very very very long string, why not loaded the data to the server. Alamofire can do it easily, just see the uploading file part.

michael wang
  • 553
  • 3
  • 11