William Larson
William Larson

Reputation: 593

Store and retrieve an array of images

I am having issues storing and retrieving an array of images by using NSUserDefaults.

var MyStringArray: [String] = []

var MyImageArray: [UIImage] = []

//to store an array of strings

NSUserDefaults.standardUserDefaults().setObject(MyStringArray, forKey: "\(FriendFullname.text!) strings")


//to retrieve an array of strings

let tabledata = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!) strings")
MyStringArray = tabledata as! [String]


//to store an array of images

NSUserDefaults.standardUserDefaults().setObject(MyImageArray, forKey: "\(FriendFullname.text!) images")


//to retrieve an array of images


let tabledata2 = NSUserDefaults.standardUserDefaults().arrayForKey("\(identity.text!) images")
MyArray = tabledata as! [UIImage]

Storing strings work fine, however Everytime I run the code for storing and retrieving the images, the application crashes.

I believe it may be because this is not the correct method to save images.

I use a different way to store single images:

// storing single images

let imageData = UIImageJPEGRepresentation(myImgVw.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()


//retrieving single images

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


// file path

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

        return fullPath
    }

So my question is how can I store an array of images into a variable of [UIImage] type.

This is the error I get when trying to store the array:

2015-05-09 16:16:02.864 Application[3310:767507] Property list invalid for format: 200 (property lists cannot contain objects of type 'CFType') 2015-05-09 16:16:02.866 Application[3310:767507] Attempt to set a non-property-list object ( ", {0, 0}" ) as an NSUserDefaults/CFPreferences value for key Frank Rosenberg images 2015-05-09 16:16:02.870 Application[3310:767507] * Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object ( ", {0, 0}" ) for key Frank Rosenberg images' * First throw call stack: (0x18298c2d8 0x1941b80e4 0x18298c218 0x1829c6064 0x182918b24 0x182917b04 0x1829c6360 0x1829c58f8 0x1829c9188 0x1837b6960 0x10011ec00 0x10011ed00 0x187401404 0x1873ea4e0 0x187400da0 0x187400a2c 0x1873f9f68 0x1873cd18c 0x18766e324 0x1873cb6a0 0x182944240 0x1829434e4 0x182941594 0x18286d2d4 0x18c0836fc 0x187432fac 0x1000f3518 0x194836a08) libc++abi.dylib: terminating with uncaught exception of type NSException

Upvotes: 0

Views: 896

Answers (2)

Ruchish Shah
Ruchish Shah

Reputation: 319

Remember whenever you get this kind of error : "Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: Attempt to insert non-property list object for key" . It is due to you can store image directly into NSUserDefaults as it is not key-value.

To achieve this you have to convert your UIImage into NSData and then you have to store image data by associating with key. When you retrieve data using you need to convert it into UIImage and then you can use it.

Example given below: to store image and retrieve.

+(void)saveImageInUserDefaults:(UIImage*)img key:(NSString *)key
{
    NSData* pictureData = UIImagePNGRepresentation(img);
    NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:pictureData];
    [[appDelegate userDefaults] setObject:encodedObject forKey:key];
    [[appDelegate userDefaults] synchronize];
}

+(UIImage*)getImageFromUserDefaultsWithKey:(NSString *)key
{
    NSData *encodedObject = [[appDelegate userDefaults] objectForKey:key];
    if (encodedObject)
    {
    NSData *imgData = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject];
    UIImage *img=[UIImage imageWithData:imgData];
    return img;
    }
    return nil;
}

Upvotes: 1

Mundi
Mundi

Reputation: 80265

I am guessing it might be because the identity property ends up being nil. You would then be trying to insert a value for a null key.

Upvotes: 0

Related Questions