iosswiftobjective-cuiimagecgimage

Loading UIImage and deleting original file


I'm trying to load an image store in locally and then delete original file.

so here the code:

UIImage *workingCopy = [UIImage imageWithContentsOfFile:workingCopyPath];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error = nil;
[fileManager removeItemAtPath:workingCopyPath error:&error];

after that I load this image into my view (OpenGL). Image looks like black square. Without deleting original file everything works perfect.

Is there any way to load image fully to memory and delete original file without damaging loaded image?

I've tried to use imageNamed but it doesn't work for me (just can't find file, probably because of extension)...

Image path is like: /Documents/SavedImages/IMG-186998396.png.working_copy

I pass NSString IMG-186998396.png.working_copy to imageNamed but it can't load the image.


Solution

  • UIImage uses caching mechanism and lazy loading (the image is note loaded unless it's rendered). Maybe postponing the deletion after it's displayed, or force it to render by drawing it to a CGGraphicContext

    Better yet : you can use a background thread for doing the force loading, see the answer on that question