Search code examples
iosobjective-cxcodeimagephotos

iOS store image in photos and save its path in string in objective c


I am using TUC Library to store image in a custom named album in photos but i also need to save the path of that stored image on successful creation to display on chat interface, code I am using is as following:

import "UIImage+TUCAssetsHelper.h"

[image tuc_saveToAlbumWithAlbumName:@"SUPImages"];
//[image tuc_saveToAlbumWithAlbumName:@"any album name here"];

[image tuc_saveToAlbumWithAlbumName:@"SUPImages" success:^{
    NSLog(@"save to custom name album: success!");
} failure:^(TUCAssetsHelperAuthorizationStatus status) {
    NSLog(@"save to custom name album: Denied!");
}];

Solution

  • This is not how it works, not even in theory.

    You must treat your photos library much like a remote server. You need to fetch images and display them as they are on library else you are going to have a bad time. For instance what do you expect it will happen if user deletes the image from photos library?

    Anyway you need to request images and never save a file path nor URL even if you get it. You may not do that even for resource or documents directory of your own application because the base path may change (migrating to new iPhone). In case of your own file system you must save a suffix of path only so you have libraryDirectory()/myFiles/mySavedFileName. Then there is iCloud which I will not even go into.

    So in case of matching images in photos library you should try to do the same as if the image was on server which means you will need to match it by some ID. Check if you can find unique identifiers for images in your photos library and save/request them. Naturally if you are going down that hole I suggest you to detach yourself from any open sources like the one you mentioned.

    On the other side I would suggest you not to save images into photo library in the first place. If you do save them there also save/cache them in your application documents or library directory. Then use these local images in your application. Again make sure not to save full paths but just relative ones or you will have problems again.