Can I async UIImageJPEGRepresentation writeToFile? - ios

I am doing the following when I get an image back from the server. But this code is bringing my app to its knees. It freezes up the UI.
Can this be run on a background thread in iOS? Can I use async?
if (![NSString isEmpty:user.avatarURL])
{
NSString *pathToImage = user.avatarURL;
NSURL *url = [NSURL URLWithString:pathToImage];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *image = [[UIImage alloc] initWithData:data];
[[NSUserDefaults standardUserDefaults] setObject:UIImageJPEGRepresentation(image, 1) forKey:kUserImage];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString* path = [documentsDirectory stringByAppendingPathComponent:#"userAvatar.png" ];
NSData* jpegdata = UIImageJPEGRepresentation(image, 1);
[jpegdata writeToFile:path atomically:YES];
}

As #valentin says, you can do everything inside the if() statement in a dispatch_async() call.
Note I suspect what’s probably slowing you down is actually the -dataWithContentsOfURL:, not the UIImageJPEGRepresentation(), so you’ll want to make sure that’s inside your dispatch_async, not outside.
Also, I’m not clear why you’re decompressing the data into an image, then re-compressing it. You’re going to get artifacts doing this, and with most services the avatar image is going to be compressed anyhow.
I’d do the following:
if (user.avatarURL)
dispatch_async(dispatch_get_main_queue(), ^{
NSURL *const imageURL = [NSURL URLWithString:user.avatarURL];
if (!imageURL)
return;
NSData *const imageDdata = [NSData dataWithContentsOfURL:imageURL];
if (!imageDdata.length)
return;
[[NSUserDefaults standardUserDefaults] setObject:imageDdata forKey:kUserImage];
[imageDdata writeToFile:[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:#"userAvatar.png"] atomically:YES];
});

The line which is doing most damage is:
NSData *data = [NSData dataWithContentsOfURL:url];
Because it does the download from the network. Nothing in your code updates the UI so it can all be run on a background thread. Just ensure that if you post a notification or subsequently update the UI after the image is saved that you switch back to the main thread.

Related

Saving image from a URL asynchronously

So i have already gone through almost all questions on SO, and I have put them all together to create a method that accepts two paramaters, first is the URL of the image to download and display in UIImageView and second is the placeholder image for that UIImageView. I want to save the image so that it won't be downloaded every time. I have used SDWebImage to download the image, however i had some confusion when it came to saving the image in documents directory using SDWebImage, so i decided not to use it. I used dispatch_sync(dispatch_get_main_queue() and my method now looks like :
- (void)saveImageFromURL:(UIImage*)image:(NSURL*)imageURL {
NSString *url = [imageURL absoluteString];
NSArray *parts = [url componentsSeparatedByString:#"/"];
NSString *filename = [parts lastObject];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#.png", filename]];
BOOL fileExists = [[NSFileManager defaultManager] fileExistsAtPath:fullPath];
if (fileExists) {
NSLog(#"File already exists");
_myUIImage.image = [UIImage imageNamed:fullPath];
return;
}
else {
dispatch_async(dispatch_get_main_queue(), ^{
[self.myUIImage sd_setImageWithURL:imageURL placeholderImage:image];
UIImage *imageFromURL = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
NSData *imageDataNew = UIImagePNGRepresentation(imageFromURL);
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager createFileAtPath:fullPath contents:imageDataNew attributes:nil];
});
}
}
I have a couple of questions, is this implementation good enough since i am working on a app that will be on app store ? Will the downloading of the image from URL be done asynchronously ? (i know i am using dispatch_async but just need to confirm). If yes, then this wont block my UI, right ?
dispatch_async(dispatch_get_global_queue( DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void){
// *** Load Image from URL Asynchronously without blocking Main thread ***
UIImage *imageFromURL = [UIImage imageWithData:[NSData dataWithContentsOfURL:imageURL]];
dispatch_async(dispatch_get_main_queue(), ^(void){
// *** Create file path to DocumentDirectory ***
NSString * docDirPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString *filePath = [docDirPath stringByAppendingPathComponent:#"myImage.jpg"]
// *** Write Image to disk ***
BOOL isSaved = [UIImageJPEGRepresentation(image, 1.0f) writeToFile:dirPath atomically:YES];
if(isSaved)
{
NSLog(#"Image write to disc successfully.");
}
});
});

ios save Image Offline

I have an application that receive images with webservices and show them in a list like for example a movie theater schedule
My question is : Is it possible to store the images in core data or something else so i can show them when the user is not connected to internet ?
Yes you can.
// Save image to disk
NSString *documentaryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/Image.png",documentaryPath];
NSData *data = [NSData dataWithData:UIImagePNGRepresentation(YOUR_IMAGE)];
[data writeToFile:filePath atomically:YES];
// Retrieve the Image
- (NSData *) imageData {
NSString *docDir = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *pngFilePath = [NSString stringWithFormat:#"%#/Image.png",docDir];
NSData *dataImage = [NSData dataWithContentsOfFile:pngFilePath];
return dataImage;
}
And later
UIImage *image = [UIImage imageWithData:imageData]
Yes it's possible.
You have to download and save each received image in the application directory, then you save in CoreData the path to those images.

Downloading a zip file in iOS

Try to download a zip file from the given URL, since the zip file Gets updated daily I wrote the codes in ApplicationDidFinishLunching method but it doesn't work. anything wrong with my code?
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// Override point for customization after application launch.
NSString *stringURL = #"http://webspace.apiit.edu.my/intake-timetable/download_timetable/timetableXML.zip";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Save the data
NSString *dataPath = [path stringByAppendingPathComponent:#"timetableXML.zip"];
dataPath = [dataPath stringByStandardizingPath];
[urlData writeToFile:dataPath atomically:YES];
return YES;
}
Try this instead as it will download the zip file in the background. Even though the file is only a few kb the download can take some time and as the download is performed on the main thread this will prevent the app from launching!
dispatch_queue_t queue = dispatch_get_global_queue(0,0);
dispatch_async(queue, ^{
NSLog(#"Beginning download");
NSString *stringURL = #"http://webspace.apiit.edu.my/intake-timetable/download_timetable/timetableXML.zip";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
//Find a cache directory. You could consider using documenets dir instead (depends on the data you are fetching)
NSLog(#"Got the data!");
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *path = [paths objectAtIndex:0];
//Save the data
NSLog(#"Saving");
NSString *dataPath = [path stringByAppendingPathComponent:#"timetableXML.zip"];
dataPath = [dataPath stringByStandardizingPath];
[urlData writeToFile:dataPath atomically:YES];
});
You should never download anything on the main thread as it will "block" your application. It should however be noted that there are better ways than this to download data. Read for example this.

Objective C: Keep Image After App Termination

I am making an app that downloads files from an online server by clicking an Icon representing the file to be downloaded, then the file will be saved to the app's sandbox.
I have managed to do those mentioned above. Now, what I want to do is to put a "Check" image on the icon to allude the user that it is already downloaded.
I already tried it by putting another UIImageViewon top of the icon when the download is successful but when I fully terminate the app and re-open it, the image is gone and I need to download it again to show it.
How will I able to that?
Just keep an array of all the downloaded files, and at startup check to see which files to apply the check mark to.
For example:
When you get the image:
NSMutableArray *array;
array = [[NSUserDefaults standardUserDefaults] objectForKey:#"downloaded"].mutableCopy;
if (!array) array = [[NSMutableArray alloc] init];
[array addObject:somethingToIdentifyTheImageBy]; //For example, a string with the name of the image
[[NSUserDefaults standardUserDefaults] setObject:array forKey:#"downloaded"]
On startup:
NSArray *downloadedImages = [[NSUserDefaults standardUserDefaults] objectForKey:#"downloaded"];
forin (NSString *string in downloadedImages)
{
//Apply check mark
}
Use following code to Save your Downloaded Image to Library Directory of Device and Retrive it, as NSUserDefaults will not allow you to store Large amount of Data.
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
NSString *libraryDirectory = paths[0];
NSString *imageFilePath = [libraryDirectory stringByAppendingPathComponent:#"myImageIcon.jpg"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:imageFilePath]) {
// File Exist at Library Directory, Just Read it.
NSData *imageData = [NSData dataWithContentsOfFile:imageFilePath];
}
else
{
// File Doesn't Exist at Library Directory, Download it From Server
NSURL *url = [NSURL URLWithString:#"YOUR Image File URL"];
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSString *imagePath = [libraryDirectory stringByAppendingPathComponent:[url lastPathComponent]];
NSLog(#"Image Stored At %#",imagePath);
[imageData writeToFile:imagePath atomically:YES];
}
You cannot save anything inside the app's bundle, but you can store the image in your app's documents directory by using [NSData dataWithContentsOfURL:] method. This is not exactly permanent, but it stays there at least until the user deletes the app.
NSData *imageData = [NSData dataWithContentsOfURL:myImageURL];
NSString *imagePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0] stringByAppendingPathComponent:#"/myImage.png"];
[imageData writeToFile:imagePath atomically:YES];

how to download files from url and store in document folder [duplicate]

This question already has answers here:
iOS download and save image inside app
(11 answers)
Closed 9 years ago.
I created one application that has two page (first page for show list of data and second page for show detail data).
when click on any cell go to next page and in next page exists one button with name : DOWNLOAD
that I want when I click on that button this file download and save in document folder.
I dont know about it. please guide me that how download any file and store in document folder.
I searching in internet but I dont understand about it.
please tell me with code that how downloaded any file with one button. Im sorry if I not good english.
It is this simple my friend,
NSString *stringURL = #"http://www.somewhere.com/thefile.png";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.png"];
[urlData writeToFile:filePath atomically:YES];
}
it advisable to execute the code in a separate thread.
EDIT 1: more info
1) for large file downloads,
-(IBAction) downloadButtonPressed:(id)sender;{
//download the file in a seperate thread.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSLog(#"Downloading Started");
NSString *urlToDownload = #"http://www.somewhere.com/thefile.png";
NSURL *url = [NSURL URLWithString:urlToDownload];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"filename.png"];
//saving is done on main thread
dispatch_async(dispatch_get_main_queue(), ^{
[urlData writeToFile:filePath atomically:YES];
NSLog(#"File Saved !");
});
}
});
}

Resources