Why saving and loading UIImages in documents folder doesn't work properly in iOS 8+?

依然范特西╮ 提交于 2020-01-07 03:41:34

问题


I've been trying to save and load UIImages in my app's Documents folder or preferably temp folder (so every time my app gets closed images will be deleted) but they aren't working correctly. I've tried three approaches but only the third approach works and i'm wondering what's the right way to do it? I highly appreciate any help.

 //First Approach, to save and load later, Doesn't work.
 //Saving the image
 NSURL *documentsDirectoryURL = [[[NSFileManager defaultManager]
                                  URLsForDirectory:NSDocumentDirectory
                                   inDomains:NSUserDomainMask]
                                    lastObject];
NSString*fileDirectoryStr = [[documentsDirectoryURL path] stringByAppendingPathComponent:@"E1BG.png"];
NSURL *url = [NSURL URLWithString:fileDirectoryStr];
//Saving either as Jpeg
[UIImagePNGRepresentation(finalFG) writeToFile:fileDirectoryStr atomically:YES];
//Or as PNG, because I need the transparent part to be kept transparent but non of them work.
[UIImageJPEGRepresentation(finalFG, 1.0) writeToFile:fileDirectoryStr atomically:YES];
//Loading image
UIImage *loadedBGImg = [UIImage imageWithData: [NSData dataWithContentsOfURL:url]];
//Returns nil

//Second approach.
//Saving the image
NSArray*pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString * documentsPath = [pathArray objectAtIndex:0];
NSString* fileDirectoryStr = [documentsPath stringByAppendingPathComponent:@"E1FG.png"];
//Again saving either as Jpeg
[UIImagePNGRepresentation(finalFG) writeToFile:fileDirectoryStr atomically:YES];
//Or as PNG, because I need the transparent part to be kept transparent but non of them work.
[UIImageJPEGRepresentation(finalFG, 1.0) writeToFile:fileDirectoryStr atomically:YES];
NSURL *url1 = [NSURL URLWithString:fileDirectoryStr];
//Loading image
UIImage *loadedImg = [UIImage imageWithData: [NSData dataWithContentsOfURL:url1]];

 //Third approach, Works but doesnt seem to be very efficient.
 NSString *tmpDirectory = NSTemporaryDirectory();
 //Save
 NSString *tmpFileStrFG = [tmpDirectory stringByAppendingPathComponent:@"E1FG.png"];
 NSURL *urlFG = [NSURL URLWithString:tmpFileStrFG];
 NSData *imgDataFG = UIImagePNGRepresentation(finalFG);
 [imgDataFG writeToURL:urlFG atomically:YES];
 //Load
 UIImage *ImggFG = [UIImage imageWithData: imgDataFG];

回答1:


I have created class which allows to save in document folder

Here is .h File

@interface FileUtility : NSObject {

}

// Folder methods
+ (NSString*)basePath;
+ (void)createFile:(NSString *)filename;
+ (void)createFile:(NSString *)filePath withData: (NSData *) imgData;

@end

and .m File

#import "FileUtility.h"

@implementation FileUtility

// ----------------------------------------------------------------------------

#pragma mark -
#pragma mark Path methods

// -----------------------------------------------------------------------------

+ (NSString*)basePath {
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSAssert([paths count] == 1, @"");
    return [paths objectAtIndex:0];     
}

// -----------------------------------------------------------------------------




  + (void)createFile:(NSString *)filename {
    NSFileManager* manager = [NSFileManager defaultManager];
    NSString * filePath = [NSString stringWithFormat:@"%@/%@", [FileUtility basePath],filename];
    DLOG(@"filePath = %@", filePath);
    if (![FileUtility fileExists:filePath]){
        [manager createFileAtPath:filePath contents:nil attributes:nil];
    }

    }

// -----------------------------------------------------------------------------

+ (void)createFile:(NSString *)filePath withData: (NSData *) imgData {
    NSFileManager* manager = [NSFileManager defaultManager];
    DLOG(@"filePath = %@", filePath);
    if (![FileUtility fileExists:filePath]){
        [manager createFileAtPath:filePath contents:imgData attributes:nil];
    }
}

For fetch use this

[[FileUtility basePath] stringByAppendingPathComponent

EDIT

+ (BOOL)fileExists:(NSString *)filename {
    NSFileManager* manager = [NSFileManager defaultManager];
    return [manager fileExistsAtPath:filename];
}



回答2:


before reading the file, make sure it exists there. If It does exist the the issue is in [NSData dataWithContentsOfURL:url] else image is not being converted to data.

I would recommend, break your code into several statement instead of of writing single lengthy statement for better understanding of where it is failing.



来源:https://stackoverflow.com/questions/45961121/why-saving-and-loading-uiimages-in-documents-folder-doesnt-work-properly-in-ios

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!