82

是否可以将图像保存NSUserDefaults为对象,然后检索以供进一步使用?

4

13 回答 13

139

在 NSUserDefaults 中保存图像:

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:key];

从 NSUserDefaults 中检索图像:

NSData* imageData = [[NSUserDefaults standardUserDefaults] objectForKey:key];
UIImage* image = [UIImage imageWithData:imageData];
于 2011-07-11T10:06:25.640 回答
85

注意力!如果您在 iOS8/XCODE6 下工作,请在下面查看我的更新

对于那些仍在寻找答案的人来说,在 NSUserDefaults 中保存图像的“建议”方式的代码。你不应该将图像数据直接保存到 NSUserDefaults 中!

写入数据:

// Get image data. Here you can use UIImagePNGRepresentation if you need transparency
NSData *imageData = UIImageJPEGRepresentation(image, 1);

// Get image path in user's folder and store file with name image_CurrentTimestamp.jpg (see documentsPathForFileName below)
NSString *imagePath = [self documentsPathForFileName:[NSString stringWithFormat:@"image_%f.jpg", [NSDate timeIntervalSinceReferenceDate]]];

// Write image data to user's folder
[imageData writeToFile:imagePath atomically:YES];

// Store path in NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:imagePath forKey:kPLDefaultsAvatarUrl];

// Sync user defaults
[[NSUserDefaults standardUserDefaults] synchronize];

读取数据:

NSString *imagePath = [[NSUserDefaults standardUserDefaults] objectForKey:kPLDefaultsAvatarUrl];
if (imagePath) {
    self.avatarImageView.image = [UIImage imageWithData:[NSData dataWithContentsOfFile:imagePath]];
}

文件路径文件名:

- (NSString *)documentsPathForFileName:(NSString *)name {
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsPath = [paths objectAtIndex:0];

    return [documentsPath stringByAppendingPathComponent:name];
}

对于 iOS8/XCODE6 正如下面评论中提到的 tmr 和 DevC 一样,xcode6/ios8 存在问题。xcode5 和 xcode 6 安装过程的区别在于 xcode6每次在 xcode 中运行后都会更改应用程序 UUID(参见路径中突出显示的部分:/var/mobile/Containers/Data/Application/ B0D49CF5-8FBE-4F14-87AE-FA8C16A678B1 /Documents/图像.jpg)。

所以有2个解决方法:

  1. 跳过这个问题,因为一旦应用程序安装在真实设备上,它就永远不会改变 UUID(事实上它确实如此,但它是新应用程序)
  2. 保存所需文件夹的相对路径(在我们的例子中为应用程序的根目录)

这是作为奖励的快速版本的代码(使用第二种方法):

写入数据:

let imageData = UIImageJPEGRepresentation(image, 1)
let relativePath = "image_\(NSDate.timeIntervalSinceReferenceDate()).jpg"
let path = self.documentsPathForFileName(relativePath)
imageData.writeToFile(path, atomically: true)
NSUserDefaults.standardUserDefaults().setObject(relativePath, forKey: "path")
NSUserDefaults.standardUserDefaults().synchronize()

读取数据:

let possibleOldImagePath = NSUserDefaults.standardUserDefaults().objectForKey("path") as String?
if let oldImagePath = possibleOldImagePath {
    let oldFullPath = self.documentsPathForFileName(oldImagePath)
    let oldImageData = NSData(contentsOfFile: oldFullPath)
    // here is your saved image:
    let oldImage = UIImage(data: oldImageData)
}

文件路径文件名:

func documentsPathForFileName(name: String) -> String {
    let paths = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true);
    let path = paths[0] as String;
    let fullPath = path.stringByAppendingPathComponent(name)

    return fullPath
}
于 2014-02-16T11:16:44.223 回答
30

虽然可以将 a 保存UIImageNSUserDefaults,但通常不建议这样做,因为它不是保存图像的最有效方式;一种更有效的方法是将图像保存在应用程序的Documents Directory.

出于这个问题的目的,我附上了您问题的答案,以及更有效的保存UIImage.


NSUserDefaults(不推荐)

保存到 NSUserDefaults

此方法允许您将任何内容保存UIImageNSUserDefaults.

-(void)saveImageToUserDefaults:(UIImage *)image ofType:(NSString *)extension forKey:(NSString *)key {
    NSData * data;

    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        data = UIImagePNGRepresentation(image);
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"]) {
        data = UIImageJPEGRepresentation(image, 1.0);
    }

    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setObject:data forKey:key];
    [userDefaults synchronize];
}

这就是你所说的:

[self saveImageToUserDefaults:image ofType:@"jpg" forKey:@"myImage"];
[[NSUserDefaults standardUserDefaults] synchronize];

从 NSUserDefaults 加载

此方法允许您UIImageNSUserDefaults.

-(UIImage *)loadImageFromUserDefaultsForKey:(NSString *)key {
    NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
    return [UIImage imageWithData:[userDefaults objectForKey:key]];
}

这就是你所说的:

UIImage * image = [self loadImageFromUserDefaultsForKey:@"myImage"];

更好的选择

保存到文档目录

此方法允许您将任何内容保存UIImageDocuments Directory应用程序内。

-(void)saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    if ([[extension lowercaseString] isEqualToString:@"png"]) {
        [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil];
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) {
        [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil];
    } else {
        NSLog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension);
    }
}

这就是你所说的:

NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
[self saveImage:image withFileName:@"Ball" ofType:@"jpg" inDirectory:documentsDirectory];

从文档目录加载

此方法允许您UIImage从应用程序的Documents Directory.

-(UIImage *)loadImageWithFileName:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath {
    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, [extension lowercaseString]]];

    return result;
}

这就是你所说的:

NSString * documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage * image = [self loadImageWithFileName:@"Ball" ofType:@"jpg" inDirectory:documentsDirectory];

另一种选择

将 UIImage 保存到照片库

此方法允许您将任何内容保存UIImage到设备的Photo Library,并调用如下:

UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);

将多个 UIImage 保存到照片库

此方法允许您将多个保存UIImages到设备的Photo Library.

-(void)saveImagesToPhotoAlbums:(NSArray *)images {
    for (int x = 0; x < [images count]; x++) {
        UIImage * image = [images objectAtIndex:x];

        if (image != nil) UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
    }
}

这就是你所说的:

[self saveImagesToPhotoAlbums:images];

images你是由哪里NSArray组成的UIImages

于 2015-02-10T02:43:06.037 回答
11

对于斯威夫特 4

我几乎尝试了这个问题的所有内容,但没有人适合我。我找到了我的解决方案。首先,我为 UserDefaults 创建了一个扩展,如下所示,然后只调用了 get 和 set 方法。

extension UserDefaults {
    func imageForKey(key: String) -> UIImage? {
        var image: UIImage?
        if let imageData = data(forKey: key) {
            image = NSKeyedUnarchiver.unarchiveObject(with: imageData) as? UIImage
        }
        return image
    }
    func setImage(image: UIImage?, forKey key: String) {
        var imageData: NSData?
        if let image = image {
            imageData = NSKeyedArchiver.archivedData(withRootObject: image) as NSData?
        }
        set(imageData, forKey: key)
    } 
}

在 settingsVC 中将图像设置为背景,我使用了下面的代码。

let croppedImage = cropImage(selectedImage, toRect: rect, viewWidth: self.view.bounds.size.width, viewHeight: self.view.bounds.size.width)

imageDefaults.setImage(image: croppedImage, forKey: "imageDefaults")

在 mainVC 中:

let bgImage = imageDefaults.imageForKey(key: "imageDefaults")!
于 2018-04-19T22:09:02.310 回答
8

对于 Swift 2.2

储藏:

NSUserDefaults.standardUserDefaults().setObject(UIImagePNGRepresentation(chosenImage), forKey: kKeyImage)

要检索:

if let imageData = NSUserDefaults.standardUserDefaults().objectForKey(kKeyImage),
            let image = UIImage(data: imageData as! NSData){
            // use your image here...
}
于 2016-04-01T12:25:27.720 回答
6

是的,在技术上是可行的

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(image) forKey:@"foo"];

但不建议使用 plist,因为 plist 不适合存放大量二进制数据,尤其是用户首选项。最好将图像保存到用户文档文件夹并将对该对象的引用存储为 URL 或路径。

于 2011-07-11T10:12:52.543 回答
5

适用于Swift 3JPG格式

注册默认图像:

UserDefaults.standard.register(defaults: ["key":UIImageJPEGRepresentation(image, 100)!])

保存图片 :

UserDefaults.standard.set(UIImageJPEGRepresentation(image, 100), forKey: "key")

加载图像:

let imageData = UserDefaults.standard.value(forKey: "key") as! Data
let imageFromData = UIImage(data: imageData)!
于 2017-05-16T08:13:43.560 回答
3

这在技术上是可行的,但不可取。而是将图像保存到磁盘。NSUserDefaults 适用于小型设置,而不是大型二进制数据文件。

于 2011-07-11T12:32:18.033 回答
3

从苹果文档中,

NSUserDefaults 类提供方便的方法来访问常见类型,例如浮点数、双精度数、整数、布尔值和 URL。默认对象必须是一个属性列表,即以下实例(或集合实例的组合):NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary。如果要存储任何其他类型的对象,通常应该将其归档以创建 NSData 的实例。

您可以像这样保存图像:-

[[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation([UIImage imageNamed:@"yourimage.gif"])forKey:@"key_for_your_image"];

并像这样阅读:-

 NSData* imageData = [[NSUserDefaults standardUserDefaults]objectForKey:@"key_for_your_image"];
    UIImage* image = [UIImage imageWithData:imageData];
于 2014-02-13T09:06:40.473 回答
1

是的,你可以使用。但是由于它是用于存储首选项,因此您可以更好地将图像保存到文件夹中。

如果需要,您可以在 中包含路径NSUserDefaults

于 2011-07-11T10:07:14.597 回答
1

将图像保存到 NSUserDefault:

NSData *imageData; 
// create NSData-object from image
imageData = UIImagePNGRepresentation([dic objectForKey:[NSString stringWithFormat:@"%d",i]]); 
// save NSData-object to UserDefaults
[[NSUserDefaults standardUserDefaults] setObject:imageData forKey:[NSString stringWithFormat:@"%d",i]];

从 NSUserDefault 加载图像:

NSData *imageData;
// Load NSData-object from NSUserDefault
imageData = [[NSUserDefaults standardUserDefaults] valueForKey:[NSString stringWithFormat:@"%d",i]];
// get Image from NSData
[image setObject:[UIImage imageWithData:imageData] forKey:[NSString stringWithFormat:@"%d",i]];
于 2011-07-11T10:23:44.240 回答
1

由于这个问题的谷歌搜索索引很高 - 这是@NikitaTook 在当今时代的答案,即 Swift 3 和 4(带有异常处理)。

注意:此类仅用于读取和写入 JPG 格式的图像到文件系统。这些Userdefaults东西应该在它之外处理。

writeFile接受您的 jpg 图像的文件名(扩展名为 .jpg)及其UIImage本身,如果能够保存则返回 true,否则如果无法写入图像则返回 false,此时您可以将图像存储Userdefaults在将是您的备份计划,或者只是再试一次。该readFile函数接受图像文件名并返回 a UIImage,如果找到传递给此函数的图像名称,则返回该图像,否则它仅从应用程序的资产文件夹返回默认占位符图像(这样可以避免令人讨厌的崩溃或其他奇怪的行为)。

import Foundation
import UIKit

class ReadWriteFileFS{

    func writeFile(_ image: UIImage, _ imgName: String) -> Bool{
        let imageData = UIImageJPEGRepresentation(image, 1)
        let relativePath = imgName
        let path = self.documentsPathForFileName(name: relativePath)

        do {
            try imageData?.write(to: path, options: .atomic)
        } catch {
            return false
        }
        return true
    }

    func readFile(_ name: String) -> UIImage{
        let fullPath = self.documentsPathForFileName(name: name)
        var image = UIImage()

        if FileManager.default.fileExists(atPath: fullPath.path){
            image = UIImage(contentsOfFile: fullPath.path)!
        }else{
            image = UIImage(named: "user")!  //a default place holder image from apps asset folder
        }
        return image
    }
}

extension ReadWriteFileFS{
    func documentsPathForFileName(name: String) -> URL {
        let paths = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)
        let path = paths[0]
        let fullPath = path.appendingPathComponent(name)
        return fullPath
    }
}
于 2017-10-02T18:20:59.250 回答
1

斯威夫特 4.x
Xcode 11.x

func saveImageInUserDefault(img:UIImage, key:String) {
    UserDefaults.standard.set(img.pngData(), forKey: key)
}

func getImageFromUserDefault(key:String) -> UIImage? {
    let imageData = UserDefaults.standard.object(forKey: key) as? Data
    var image: UIImage? = nil
    if let imageData = imageData {
        image = UIImage(data: imageData)
    }
    return image
}
于 2019-12-30T08:33:22.090 回答