0

I would like to take an image from a URL:

http://imgur.com/gallery/ihHQvP1

and then convert it NSData so that I will be able to parse it up to the server. The server only accepts NSData and not image links. I have the following from the camera:

 NSData *pictureData = UIImagePNGRepresentation([self scaleImage:activityImageView.image toSize:CGSizeMake(230, 230)]);

it is then put into a JSON format to send to the server:

jsonPostBody= [NSString stringWithFormat:@"{\"activity_id\":\"%@\",\"user_id\":\"%@\",\"activity_title\":\"%@\",\"activity_category_id\":\"%@\",\"activity_description\":\"%@\",\"activity_image\":\"%@\",\"activity_begindate\":\"%@\",\"activity_enddate\":\"%@\", \"activity_time\":\"%@\", \"activity_email\":\"%@\",\"activity_city\":\"%@\",\"activity_where\":\"%@\",\"gotooffer_url\":\"%@\",\"latitude\":\"%@\",\"longitude\":\"%@\"}",[editActivityDict valueForKey:@"activity_id"],singletonObj.userID,title,selectedCategoryString,discription,[Base64 encode:pictureData],begindateConvert,enddateConvert,milatryTime,@"",city,where,entersite,singletonObj.singletonlatitude,singletonObj.singletonlogitude];
                    NSString *urlString=[NSString stringWithFormat:@"%@%@",ROOT_URL,EDIT_ACTIVITY];

As you can see the variable for the picture being sent in the body post is:

[Base64 encode:pictureData]

Any Ideas?

1 Answers1

0

The simplest way is to use +[NSData dataWithContentsOfURL:options:error:] to fetch the data returned by performing a GET request to that URL as a NSData instance. This, however, perfoms a synchronous web request, which is not recommended to be performed on the main thread to avoid blocking the UI.

To avoid a synchronous fetch on the main thread, you could put the same code in a background thread using GCD or operation queues, but the safest way is to use NSURLConnection/NSURLRequest as is described in this response.

Community
  • 1
  • 1
matehat
  • 4,906
  • 2
  • 25
  • 40