Using Restkit and Core data to Retrieve Images - iphone

I have a core data application using rest kit to retrieve data from a web service. The response i get from the webservice is a json string like this -
"nodeRef": "workspace:\/\/SpacesStore\/b1d51831-990d-47a8-a018-f1c1bg33f594",
"type": "document",
"name": "x.jpg",
"displayName": "x.jpg",
"title": "myTestProject",
(This is some meta data around an image. If i want to retrieve this image then i should access it using this url - http://x.co.uk/share/proxy/alfresco/api/node/workspace/SpacesStore/b1d51831-990d-47a8-a018-f1c1bg33f594/content/thumbnails/ipad1024?c=force) - note ive changed the URL slightly so you wont be able to access it. Also you need to authentication to access the URL. So i was wondering how i would go about getting this image and storing it locally on my SQLite DB? I can obviously access the URL but how exactly do i prompt it to download the image

What i did in this situation was to do a lazy load on the image with something like:
image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.absoluteUri]]];
NSData *imageData=UIImageJPEGRepresentation(image, JPGCompression);
Then store it. Outside of Restkit. This was a while ago, but it worked for me.
Basically extract the URL from restkit, do a lazy load when the image is presented on the screen, and thne store it back to core data (or wherever) when you have loaded it once.
Hope this helps.

Related

How does Alamofire "upload" images from Swift to a backend?

I am trying to upload images from Swift to my Django backend so that I can store them in Google Cloud Storage and access them as "foreign keys" in my database models in django. There seems to be a built in library to bridge Django models and Google Storage https://django-storages.readthedocs.io/en/latest/backends/gcloud.html.
Reference upload image to server using Alamofire.
Before sending an image to the backend, the image is converted into a UIImageJPEGRepresentation. How exactly is Alamofire then sending the image via "multipartFormData"? Is it simply sending the raw UIImageJPEGRepresentation as a string?
I am asking this because I need to somehow convert this image data I receive in my backend and store it in a Django model and simultaneously upload it to Google Storage.
From the documentation:
>>> class Resume(models.Model):
... pdf = models.FileField(upload_to='pdfs')
... photos = models.ImageField(upload_to='photos')
To store a local file to cloud storage and at the same time in a model field:
>>> obj1.pdf.save('django_test.txt', ContentFile('content'))
>>> obj1.pdf
<FieldFile: tests/django_test.txt>
Would something like this work for an image sent from Alamofire?
>>> obj1.photos.save(<Insert whatever was sent from Alamofire>)
If so, how do you access the data sent from Alamofire?

How to remove query string from Firebase Storage download URL

Problem:
I need to be able to remove all link decoration from the download URL that is generated for images in Firebase Storage.
However, when all link decoration is stripped away, the resulting link currently would return a JSON document of the image's metadata.
Context:
The flow goes as follows:
An image is uploaded to Firebase from an iOS app. Once that is done the download URL is then sent in a POST request to an external server.
The server that the URL is being sent to doesn't accept link decoration when submitting image URLs.
Goal:
Alter the Firebase Storage download URL such as it is stripped of all link decoration like so:
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg
Notes:
The problem is twofold really, first the link needs to be manipulated to remove all the link decoration. Then the behavior of the link needs to changed, since in order to return an image, you need ?alt=media following the file extension, in this case .jpg. Currently, without link decoration, using the link with my desired structure would return a JSON document of the metadata.
The current link structure is as follows:
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg?alt=media&token=[TOKEN]
Desired link structure:
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg
The token is necessary for accessing the image depending security rules in place, but can be ignored with the proper read permissions. I can adjust the rules as needed, but I still need to be able to remove the ?alt=media and still return an image.
Building up on Frank's answer, if you access to your associated Google Cloud Platform project, find the bucket in the Storage tab and make this bucket public, you will be able to get the image from here with the format you wish. That is, you will not be accessing through Firebase
https://firebasestorage.googleapis.com/v0/b/example.appspot.com/o/[FOLDER_NAME]%[IMAGE_NAME].jpg
but through Google Cloud Storage, with a link like
https://storage.googleapis.com/[bucket_name]/[path_to_image]
Once in your GCP project Console, access the Storage bucket with the same name as the one you have in your Firebase project. They are the same bucket. Then make the bucket public by following these steps. After that, you will be able to construct your links as mentioned above and they will be accessible with no token and no alt=media param. If you do not want to make the public to everyone, you will be able to play around with the permissions there as you wish.
You could split the url string into two halves by using String.componentsSeparatedByString(_ separator:)
Storage.storage().reference().child(filePath).downloadURL(completion: { (url, error) in
let urlString = url.absoluteString
let urlStringWithoutQueryString = urlString.componentsSeparatedByString("?").first!
})
Calling .downloadURL on a StorageReference will return you that URL, but this method can be used to remove the query string from any URL. String.componentsSeparatedByString(_ separator:) breaks a String into an array of Strings, splitting the string by any occurrence of a given separator, in this case ?.
NOTE this method assumes that ? occurs only once within the url string, which I believe is the case for all Firebase Storage urls.
You should treat the download URL that you get back from Firebase as an opaque string. There's no way to strip the parameters from a download URL without breaking that download URL.
If you want to allow public access to the files in your bucket with simpler URLs, consider making the object in your (or even your entire) bucket public.

Upload Multiple Images to API using Multi Image Picker

I am attempting to implement the ability to upload multiple images (along with some other data) to an API using data entered via a form on my flutter application.
I am using https://pub.dev/packages/multi_image_picker which stores all the images as a List<Asset>
The API I am attempting to connect with says it requires the following fields.
firstname, lastname and images[].
I have started to encode the json body using:
var body = json.encode({"firstname": firstNameField, "lastname": lastNameField, "images": imageList});
but this failed. Does anyone have any suggestions?
Hey #Bollie, You can do using the flutter_uploader package, it's very simple you can post your data in formdata separately with multiple files/images.
here is more info on how you can do,Hope it'll work for you.. https://github.com/BlueChilli/flutter_uploader/issues/9
feel free to ask any questions regarding this, Actually I recently did it so...

Drupal RESTful web services to access image URL using Ionic

Firstly, I'm a long time user of stackoverflow for existing answers, first time asker, so go easy if this is too basic or has been answered elsewhere.
I'm trying to use Drupal 7's RESTful web services module to serve content to an Ionic app. I have the content flowing through fine (e.g. title and body text) but am struggling to access the images. The relevant part of the JSON response is:
"field_cover_image": {
"file": {
"uri": "http://example.com/drupal/file/9",
"id": "9",
"resource": "file"
}
},
How can I translate that information for use inside an image tag, from what I can see I am only being given the ID of the image at this point where what I need is the url/filename.
Thanks
You can create path to original image file (the one user uploaded) from uri with function file_create_url() :
https://api.drupal.org/api/drupal/includes%21file.inc/function/file_create_url/7.x
And you can create path to image file in specific image style with function image_style_url(). Second parameter of this function is actually uri, not path.
https://api.drupal.org/api/drupal/modules%21image%21image.module/function/image_style_url/7.x
I didn't use Restful Web Service, so I can't tell you how to do that exactly, but you must somehow pass of this uri to one of those 2 functions to get url to image file.

Insert an image into a BIRT report with a secured URL

I wanted to load an image in my report with an URL.
The following stackoverflow question answers that.
How to insert an image into a BIRT report with an URL properly?
However, my URL is not public and requires authentication to access. Like, adding an Authorization token in the header.
How can I intercept the request to insert Authorization token?
You could add a new "Scripted Data Source" and write your request in JavaScript with the required authorization token to load the Image data into your Data Source.
Then you add a data Set to get the data from the scripted data source. Afterwards you add the Image as "Dynamic image" and load the data from the Data Set that gets the data from your scripted data source. This is a bit complicated, but it should be possible.
Please search for tutorials for creating a scripted data source by yourself. This is a complex field but opens up a lot of possibilities.