为什么在文档文件夹中保存和加载 UIImages 在 iOS 8+ 中无法正常工作? [英] Why saving and loading UIImages in documents folder doesn't work properly in iOS 8+?

查看:43
本文介绍了为什么在文档文件夹中保存和加载 UIImages 在 iOS 8+ 中无法正常工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在我的应用程序的 Documents 文件夹或最好是临时文件夹中保存和加载 UIImages(因此每次我的应用程序关闭时,图像都会被删除),但它们无法正常工作.我尝试了三种方法,但只有第三种方法有效,我想知道正确的方法是什么?我非常感谢任何帮助.

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];

推荐答案

我创建了 class 允许保存在文档文件夹中

I have created class which allows to save in document folder

这里是.h文件

@interface FileUtility : NSObject {

}

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

@end

和.m文件

#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];
    }
}

为了获取使用这个

[[FileUtility basePath] stringByAppendingPathComponent

编辑

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

这篇关于为什么在文档文件夹中保存和加载 UIImages 在 iOS 8+ 中无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆