ios9 cache for NSData? - swift

I use the following code to display UIImage loaded from Internet:
let imgURLString = "http://.../images/" + (self.userImage as String)
let imgURL = NSURL(string: imgURLString)
let imageData = NSData(contentsOfURL: imgURL!)
self.profilePictureImage.image = UIImage(data: imageData!)
That works good with the iphone5 simulator if the image is not too big.
But when I test it with my phone (ios9 iphone5), if I change the image on the server, the old image is still displayed.
I think there is a cache.
Is there a way to clear the cache? Or maybe a better approach to display images stored on a remote server?
Thank you for your feedback.
Regards,
Thomas
EDIT
I just found one interesting thing.
When I test in local network (through WIFI), it works fine with both simulator and iPhone.
But When I test remotely (through 4G), the image displayed is the old image. I have to connect the server with a web browser and refresh the images folder to make the new image display on the app.
Any idea to solve this?

I finally fixed it by changing the name of the new image stored on the remote server. Thus, I avoid any caching issue.

Related

PFImageView loadInBackground() issue in iOS10

I am trying to use PFImageView.loadInBackground() in iOS10 .
So far my code has worked in iOS9 etc.
This is what I am doing:
fetching image data
assigning data to PFImageView.file
loading file via PFImageView.file.loadInBackground()
The ImageData is found, but the image won't display.
Any ideas?
I had an issue with PFImageView when the file name for PFFile had white spaces.
Did you try running in Simulator what errors did you receive?

Saving UIImage to plist problems (SWIFT)

Simply stated,
I can encode a UIImage into a .plist if that image is selected from a UIImagePickerController (camera or photo library) and then stored into an object's instance variable using NSKeyedArchiver...
func imagePickerController(picker: UIImagePickerController!, didFinishPickingImage image: UIImage!, editingInfo: NSDictionary!) {
let selectedImage : UIImage = image
myObject.image = selectedImage
...
}
I can NOT, however, encode a UIImage into a .plist if that image is one existing in my app bundle and assigned to a variable like this...
myObject.image = UIImage(named: "thePNGImage")
...where thePNGImage.png lives in my apps bundle. I can display it anytime in my app, but I just can't store it and recover it in a plist!
I want a user to select a profile image from his or her camera or photo library, and assign a default image from my app bundle to their profile should they not choose to select one of their own. The latter is giving me issues.
Any help would be greatly appreciated.
Thanks!
First of all, you don't need any plist. If you want to save the user's preferences, use NSUserDefaults (which is a plist, but it is maintained for you).
Second, you should not be saving an image into a plist. Save a reference to an image, e.g. its URL. That way when you need it again you can find it again. An image is huge; a URL is tiny.
Finally, from the question as I understand it, you want to know whether the user has chosen an image and, if not, you want to use yours as a default. The NSUserDefault could contain a Bool value for this, or you could just use the lack of an image URL to mean "use the default".
But in any case you are certainly right that it is up to you to recover state the next time the app launches based on whatever information you have previously saved. Nothing is going to happen magically by itself. If your image is not magically coming back the next time you launch, that is because you are not bringing it back. Your app has to have code / logic to do that as it launches and creates the interface.
Still having the issue? BTW, I've confirmed your issue. Here's a workaround:
func imageFromXcassets(name: String) -> UIImage
{
// Load image from Xcassets
let myImage = UIImage(named: name)!
// Write image to file
let imagePath = NSHomeDirectory().stringByAppendingPathComponent("Documents/test.png")
UIImagePNGRepresentation(myImage).writeToFile(imagePath, atomically: true)
// Get image from file and return
return UIImage(contentsOfFile: imagePath)!
}
Given the name of an image stored in Images.xcassets, the method returns the UIImage after first writing the image to a file, then reading the image from the file. Now the image can be written successfully to NSUserDefaults just like images obtained from your imagePickerController.
Please note that the example isn't handling optionals like it should. (I'm an optimistic guy.)
ADDITION BELOW
Note on Leonardo’s response:
The originating question asks how to encode an image obtained from Images.xcassets via UIImaged(named: String). I don’t believe Leonardo’s response provides a solution for this.

jpg images are shown in iphone simulator but not in iPhone test device

i'm sorry for my english.
i'm new in iphone development and happens to me a strange things.
I have a set of jpg images to show in a table view. When i test the app in iphone simulator everything is ok and work properly but when built and run the same code in iphone test device the same images aren't displayed.
Another strange behavior is that with a set of png images instead of jpg are shown perfectly in simulator like as in iphone test device.
Anyone can suggest me a solution?
I detect the name of image to load from a json file. This is the code that i use to show the image:
UIImageView *immaginePiadina = [[UIImageView alloc] initWithFrame:immagineValueRect];
[immaginePiadina setImage:[UIImage imageNamed:[item objectForKey:#"immagine"]]];
where [item objectForKey:#"immagine"] is an element of my json file like this:
"nome": "66",
"immagine": "pianetapiadabufalavesuviani",
"prezzo": "€ 7,50",
"nomeingrediente": [
"bufala",
"vesuviani"
]
How you can see i refer to the image with only the name of the file and without the file extension. I did it in this way to show image retina, it's wrong?
I exclude that i wrote a different case sensitive name because the png set works properly.
thanks a lot!!
There are possibly two separate issues that need separation, here is how to solve your problem.
First write a method using the NSFileManager, that given a file path verifies a file exists and has a size greater than 0. Insert a call to this everywhere you fail to open an image. If you use "imageNamed" then get the path to the bundle and create the path. If this method fails to find an image, fix it.
Second, the image decoding is different for the simulator and the device - the simulator uses the full OSX libraries. So take one image that fails to open and move it somewhere. Open it in Preview and export it using the same name but with png+alpha format. Change your code to expect a png not a jpg, and retest. Make sure you still use the first method to insure the file is there.
Once you get one success you can try other options, like using Preview exported jpegs. The reason this should work is that all of these image formats permit a huge range of options all of which iOS does not support.
Given that the current format us the problem, you can script changes using the "sips" program, which is what Preview uses.
I solve my problem using the following code:
NSString* path = [[NSBundle mainBundle] pathForResource:[item objectForKey:#"immagine"] ofType:#"jpg"];
NSLog(#"%#",path);
UIImage* theImage = [[UIImage alloc] initWithContentsOfFile:path];
I don't know the reason but in this way i haven't any problem to display jpg in my iPhone test device. Maybe because NSBundle specify also the extension of the file.
Thanks #David-h that directed me in the right way to solve my problem.
Check the extensions of your images. If you write .PNG, in the simulator is Ok, but not in the device.

Sharing an image using parse.com

I would like to share an image between two users using parse.com. My original idea was to simple pass the image url from the first user to the second to view/download. I have no issue uploading the image to parse.com , however I can't work out how to return the url. Is it possible to return the url?
There seems to be a solution for this on android Get link to image file at Parse.com but I can't find one for iphone.
Thanks in advance
In the Image save example of Parse.com, I found the image url.
In - (void)setUpImages:(NSArray *)images method,
PFFile *theImage = [eachObject objectForKey:#"imageFile"]; will return image file object with its URL.
Hope this will help you.

Getting difficulty in reading image from Disk for iphone in Unity

I am developing a unity app, in which i call webservice for image url and after getting those url, i call one by one url for image downloading and store those images onto disks, and after in some point i read those images from disks and show as texture, but i am getting problem in reading images. It show me Question mark on texture and when i dig more to find out problem i got that i am getting zero of image using www.size and text also nil using www.text. I am doing following for reading and writing images.
Writing
if(wwwMarker.isDone)
File.WriteAllBytes(Application.persistentDataPath + "/"+ data.markerName + ".jpg", wwwMarker.bytes);
Reading
//fileurl is string which contain path of file
fileUrl = (Application.persistentDataPath + "/"+ markerDataObject.markerName + ".jpg");
if(System.IO.File.Exists(fileUrl))
if(www.isDone)
video.mIconPlane.renderer.material.mainTexture = imageToLoadPath.texture;
But when i read this code and show render image on texture it show me Question mark image, but when i load images from assets it works perfectly fine. Please help me that where i am doing wrong. I am nee bee in unity so thats why doing silly mistakes. This will be great for me. Thanks in advance.
Note, that unity's WWW can only dowload and save as textures JPG and PNG images. If you will try to download an image of any other format, you will get a red "?" image as result.