javal88
javal88

Reputation: 1238

How to save the user avatar in an iOS app

My app retrieve the image from a PHP json reply but how can i store the user avatar on my app? I always had to store the username in my previous work and i did that simply using the NSUserDefaults.
I read about NSCoding implementation but i didn't understand the complete flow, someone can explain that to me?

My goal is to have that image stored in order to show that quickly (i need to present that in every view) and not to load that every time from the server.

Thanks

Upvotes: 0

Views: 325

Answers (3)

yoeriboven
yoeriboven

Reputation: 3571

Extend UIImageView (create a category) and use this code. Also use UIImageView+AFNetworking.

- (void)setDownloadedImage:(NSString *)imageName{
    NSString *libraryPath = [NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) lastObject];
    NSString *localImagePath = [libraryPath stringByAppendingPathComponent:imageName];

    // Check if the image is already downloaded
    if (![[NSFileManager defaultManager] fileExistsAtPath:localImagePath]) {
        // Store image
        NSURL *serverFilePath = [NSURL URLWithString:[NSString stringWithFormat:@"%@/images/avatars/%@", SERVER_URL, imageName]];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSData *imageData = [NSData dataWithContentsOfURL:serverFilePath];
            [imageData writeToFile:localImagePath atomically:YES];

            // Switch back to the main thread to update the UI
            dispatch_sync(dispatch_get_main_queue(), ^{
                [self setImageWithURL:[NSURL fileURLWithPath:localImagePath] placeholderImage:nil];
            });
        });
    }
    else{
        [self setImageWithURL:[NSURL fileURLWithPath:localImagePath] placeholderImage:nil];
    }
}

It checks if the image is already downloaded and if it is not it will be stored and read from disk. If it is already downloaded it will also read from disk.

Upvotes: 1

Adnan Aftab
Adnan Aftab

Reputation: 14477

You can write image to the filesystem, every app in ios have document folder. You can also save image in coredata as binary object like NSData. Other than this you can save it file system and save the url in database.

At the startup load image to the memory and use it in any view. This would be the best. If you are loading avtar from server it is possible avatar can be change. You can load avatar from server and can cache it so in a session when you try to access that image again you don't need to get it from server.

Upvotes: 0

Wain
Wain

Reputation: 119031

Once you have an image you can get the image data with either UIImageJPEGRepresentation or UIImagePNGRepresentation. Once you have the image data you can save it to disk using writeToFile:options:error:. Then in the future you can load it with UIImage initWithContentsOfFile:.

Now all you need to do is store the location that you saved the image at (either hard coded or in user defaults).

Upvotes: 0

Related Questions