Curtail UIImage download size from WEB - ios

I have an application that downloads images based on URL using the call:
img = [[UIImage alloc] initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:urlToImage] options:0 error:&err]];
where URL is an image on the WEB.
I really want to curtail the amount of data transfer, so would like to reduce the file size of the download.
I was wondering if anyone has a magic way of reducing the file size of the download short of implementing a PHP server process to be the go between to take care of this.
Thanks in advance!

Related

Display image on UIImageView using Web server image

I want to display an image on UIImageView calling the Web server image.
Because I want to replace and show it right away.
here's the code
NSURL *imageURL = [NSURL URLWithString:urlString];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
webImage.image = [UIImage imageWithData:imageData];
The problem is...
The image is displayed well at first,
but if I replaced the image on the web server with another image.(The file name is the same, only the file is replaced) It didn't change and still displayed the first image.
Please help me...
The code in the question seems flawed, i.e. you apparently do a network request in the UI thread. Such approach tends to make your application UI unresponsive. However, if we boil down the question to "Why NSData returns outdated data for the same URL?" then the answer is because it has internal caching. The caching logic could controlled either by the the response headers from the server (e.g. Expires) or with NSDataReadingUncached option (to suppress any caching logic):
NSData *data = [NSData dataWithContentsOfURL:url options:NSDataReadingUncached error:nil];

how to handle online images in Parse

so I'm using Parse in my app as backend cloud service. I looked into Parse documents and see that they have PFFile method which can upload local images to Parse, and PFImageView class to retrieve remote image from Parse. But in my app, we have a lot of online images with URLs, and how can I easily display these images in my app without worrying about caches and all? Or is there any way to download and upload online images to Parse easily so that I can just use Parse's service?
Since you aren't loading images from Parse, there's no reason to use PFFile or PFImageView. Downloading and uploading them would be redundant if you know they will remain available at their respective URL's. This will allow you to load images asynchronously (in the background) from a URL:
dispatch_async(dispatch_get_global_queue(0,0), ^{
NSData * data = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: #"http://myurl/mypic.jpg"]];
if ( data == nil )
return;
dispatch_async(dispatch_get_main_queue(), ^{
UIImageView *imView = [[UIImageView alloc] initWithImage:[UIImage imageWithData: data]];
});
});
As far as cacheing goes, if your images are large you may notice a delay in their loading and may still need to cache. This is one of the most popular solutions:
https://github.com/path/FastImageCache

Big difference on file size between iOS picture on the local and on server

There is a image URL http://example.com/xxoo.jpg
Open this URL from Chrome, save image to desktop to check the image file size.
Open this URL from iPhone safari, save image to camera roll, than send it to computer to check the image file size.
Save the image in my iOS app by using UIImageWriteToSavedPhotosAlbum, and than send it to computer to check the image file size.
Strangely, these files are all different from each other. The difference can be hundreds of kb.
I had tried download image by using AFNetworking setImageWithURL, SDImageView setImageWithURL, SDImageDownloader downloadImageWithURL and dataWithContentsOfURL. These downloads are the same size but different from the size on the server.
There is an example code:
[[SDWebImageDownloader sharedDownloader] downloadImageWithURL:imageURL
options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
_imageToBeDownload = image;
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
}];
These are the possible reasons:
The camera roll may use a different file format than the original file format on the server.
SDWebImageDownloaderreturns an image not a NSDataobject containing the JPEG file from the server. When saving an image with UIImageWriteToSavedPhotosAlbum the file format can be different than that on the server.
Proxies in the Internet per default are allowed to convert the media types (in particular images) to a different one, possibly with a higher compression level in order to save space in the cache. So, a GET request may return a cached version of the original file on the server which has lower resolution. (Reference: 14.9.5 No-Transform Directive, RFC 2616)

How can I open an image from file system into UIImage View

Before anyone down votes my question, I have literally looked all over stack and cannot find the answer
I am making a phonegap app which I can place an image into my filesystem
the url :
file:///Users/danielnasello/Library/Application Support/iPhone Simulator/7.0.3/Applications/75EE3563-560D-4CFD-B357-313DD559573D/Documents/Vault/1386252707450.jpg
I can then pass this url to my modal controller to present an image in the image view.
the problem is I cannot seem to find the correct way to access the image from my filesystem and display it into my image view.
my current relevant code
(self.myImage) is the string i pass from phonegap. it does contain the url string because I am logging it, so I know the url is getting passed. However, the image simply will not display.
I tried using image named from one of my library images and it works fine. I just cant seem to find the correct way to present it using file url.
NSURL *url = [NSURL URLWithString:self.myImage];
NSData *data = [NSData dataWithContentsOfURL:url];
img = [[UIImage alloc] initWithData:data];
self.imageView.image= img;
here is the code for that.
NSString *filepath=[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"1386252707450.jpg"];
self.imageView.image=[UIImage imageWithContentsOfFile:filepath];
this code is actually correct. It just doesn't work on simulators (shocker), you need a real device

Doesn't load picture from valid URL

UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:imageURL]]];
I use the code above and then check
if (image)
return image;
else
{
NSLog(#"no image on URL");
return nil;
}
But sometimes (very very seldom) I don't get an image from a valid url. The url is valid 100%.
Usually it takes nearly one second to load a picture, but when it can't load a picture the process takes much more time (20-200 seconds).
And then i get "no image on URL".
Is there a better way to get a picture from URL?
I'd rather get "no image on URL" in one second then waiting so long.
P.S. srry for my poor english
It is very rare that you want to use dataWithContentsOfURL:. It's a blocking call so requires a background thread. It's also inflexible and doesn't provide good error returns (which is the problem you're encountering).
See the URL Loading System Programming Guide. Generally you'll want to configure an asynchronous NSURLConnection for this kind of work. If you're doing a lot of network operations, you may want to consider a framework like MKNetworkKit or AFNetworking which handle a lot of the complexities for you.

Resources