I have a gallery module, the functionality implemented in this module as below
- getting the file path from the server using ajaxrequest
- the response will be json object of all image file path
- setting the filepath in image src attribute
As we are using ajax request, the images are loading in online mode only.
so how to implement the functionality so that images should show in offline also.
You may consider returning images as base64 string from the server and store them in a localstorage.
On the view use data-ng-src directive like this .
In your controller check if there is no connection and set base64 string from the localstorage as this: $scope.data.image_url=
After loading an image once, your best bet is going to be get a base64 representation of it, and then persisting that to disk.
Get the base64 representation of the image here:
Get image data in JavaScript?
Write the base64 data to disk using ng-cordova/ionic native and the writeFile method using the Cordova file plugin.
http://ngcordova.com/docs/plugins/file/
writeFile(path, file, data, replace)
There are some great answers here that I would like to build on...
I would suggest using PouchDB as a cache for base64 and/or Blob data after you have downloaded the original (one of my apps does the same thing with mp3 data converted to a Blob). You could then implement a method that checks the cache for the image before making a network request.
Nolan Lawson has created an excellent library for these binary conversions: https://github.com/nolanlawson/blob-util
Just save the base64 string to your PouchDB instance after the initial download, you can then check for that data before your app reaches out to the network.
Just beware of storage limits on iOS Safari (~50mb default)...
Related
I'm using the image upload script with a backend URL to rename uploaded images stored on the server. It seems to work well with both file uploads and also drag-and-drop.
Without that backend handling you can paste images directly into the editor, but they get saved as base64 with the post in the db itself which is not great for a lot of reasons.
If I use the image upload script as I am now, it seems it blocks the pasting of images directly into the editor. That's safest, I guess, but I was wondering if there was a way of allowing it, but treating the pasted image like a drag-and-drop image, for example, so we could have the convenience of pasting images but still have them saved with unique filenames on the server instead of being embedded as base64 with the rest of the source of the post?
Thanks.
Okay so I want to load a image from a URL in my app, however this image will need to be updated every month manually, therefore its not practical to have to submit a new update to apple every time the url changes. So does anyone no of a picture/file sharing site that would allow me keep the same URL for the image even when uploading a updated version of the image?
You can just use the same url for the image that you want presented hosted on your domain or a hosting site.
So something like
http://www.mydomain.com/appImage.jpg
That you always call to and always is the address of the relevant image. Basically the URL is just a location that you can store any image at.
Up to my knowledge, in order to achieve this you will need a static URL. So that means you'll have to buy a domain from a hosting service.
Or if not you can use Google Drive for this. But you'll have to do some more coding for this.
Try using Google Drive:
Upload a text file(*.txt) to your Google Drive and get a shareable link. Convert it to a direct download link.
Next save whatever your image as a String (base64); I have attached some interesting coding examples below.
Then give the file link to your app; download the file; read the String(base64) convert it back to image format.
When you want to update the Image convert it to a String; open the Google Drive; edit your file using Drive Notepad (Do not use
Google Docs) and simply save it Ctrl+S.
Useful Links:
Google Drive Direct Link Generator
Convert between UIImage and Base64 string
Converting between Image and Base64 string in iOS
Java BufferedImage to PNG format Base64 String
Convert image to base64 with java
Hope this helps.
I need to download images from a website and display them on(?) multiple UIImageView.
Maybe I'll code a php to "read" the directory and search for images, write a XML file and use it as medium. But I'm not sure if it's the best way.
Let's see the options you have to fetch images from a website:
Fetching HTML and Parsing the HTML to find the images (on the iphone). Then downloading the images.
Writing a script (maybe PHP) that writes all image links to an XML file (or JSON), and then fetch the output of your script with all the links.
If you choose option (1) you'll need NSURLConnection to fetch data asynchronously (without blocking the UI). I would also use TFHpple to parse HTML using xpath queries, see this tutorial for help. Finally to fetch the images using their URLs you can use SDWebImage, SDWebimage also provides caching so your app will not download the same image multiple times.
The bad side of using option (1) is that any change in the Website you're getting the images from will break your app and you'll need to issue an update to the app store in order to fix it.
If you choose option (2), your app will be easier to fix if the website changes, you'll just need to modify your script.
If you go with option (2) you'll probably need NSURLConnection, NSXMLParser (or a third party XML parsing library) and to download the images I would recomend SDWebImage again. I would also advise using JSON (and NSJSONSerialization) instead of XML, just beacuse I find JSON easier to parse.
Yes, it will be very good if you write some php script to get image list (list of image urls).
After getting such urls you can asynchronously download and show them in image views. Look here for such async image view implementation
My app contains some images which needs to be dynamically loaded from web server through XML file. Every time when changes done in admin console at web server it should reflect in iPhone app too through the XML file. I wrote XML file but dont know how to get used in iPhone code. I referred the following links,
how-to-retrieve-data-through-xml-in-iphone-locally-file
dynamically-pulling-images-from-xml-for-iphone-app
how-to-change-the-tabbaritem-images-dynamically-in-iphone-app
retrieving-images-to-iphone-app-through-xml
but all these links are not clear for me to understand. Kindly suggest me a way to do it.
So I assume your web server can serve an XML which includes the image in some encoded format like base64. Then you need to:
Load the file. For this, you should use NSURLConnection, e.g. as described in how-to-make-http-request-from-iphone-and-parse-json-result
Parse the XML. Instead of parsing a JSON, you need to parse your XML, so first, you need an XML-parser. Here is a comparison of XML parsers. Then you should access the element in the XML that represents your image. This step depends on your choice of the parser.
Decode the Image. Your element is probably an NSString and you want to have its binary representation as NSData. NSData has some convenience functions for this. E.g. for base64, there is the dataFromBase64String: constructor.
Create the image. That is, UIImage using the imageWithData: constructor.
Display the image. You need an UIImageView and assign to it your UIImage. Set the frame of the view and add it to your preferred subview.
It is also possible that the XML provides URLs where to get the image data. Then you have to do a new request. This should be clear by now, how to do.
Good luck.
I have to link a date and a name to some jpegs that I am including in my bundle, or possibly downloading from my own server to the Documents folder. Is there a way to extract EXIF data easily?
If so, then I will use EXIF to store this info. If not, then I will have to create a database or flat file that maps my extra data to the image file.
Keep in mind, these are not photos the iPhone has taken and is providing via UIImagePicker or from outside the sandbox. These are photos that I am including with the app or downloadig to the Docs folder myself. The important point here is ease:
Is it easier to
read EXIF file from my image files
have another file that keeps track of
the image file and the associated
data (could be sqlite)
Thanks!
You can try using iphone-exif toolkit to extract the data. However, it's licensed GPL and if your app is commercial you'll need to negotiate a license deal. If that's not viable then you may want to go the external meta-data route.
The actual EXIF data is stored in the form of a small TIFF file with EXIF-specific TIFF tags for information that doesn't have a home in the TIFF specification. When placed in a JPEG file (really a JFIF bitstream), it is stored in a JPEG APP1 marker which limits the total size of the EXIF data to just a bit less than 64KB.
It shouldn't be that difficult to locate the APP1 marker, confirm it contains EXIF data, and then parse out a specific collection of EXIF tags with fairly brute force coding.
One example you can look at is exiftool which does just that, and is written in Perl and open source under the same terms as Perl itself.
If these files are purely for use in your own application and will not be reused in other tools by the user, then there is some mileage in storing your data as XML/JSON in the comment segment 0xFFFE. As mentioned before you get just short of 64k to play with.
The beauty of using the comment segment is that it should be preserved by image editing tools, is quick to access (because you do not need to traverse the IFD blocks that store EXIF data, you just read/write a text string with 4 byte type/length header) and is human readable/writable in a graphics app.
I would avoid storing the associated data in a db if practical, so that you don't risk the db becoming out of sync with the available files.
I use ExifTool
embedded in my app. Works a treat.