4

嗨,我有我的ALAsset URL保存NSMutableArray

"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=119A0D2D-C267-4B69-A200-59890B2B0FE5&ex‌​t=JPG", 
"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=92A7A24F-D54B-496E-B250-542BBE37BE8C&ex‌​t=JPG", 
"ALAsset - Type:Photo, URLs:assets-library://asset/asset.JPG?id=77AC7205-68E6-4062-B80C-FC288DF96F24&ex‌​t=JPG

我无法保存NSMutableArrayNSUserDefaults因为它有一个错误Note that dictionaries and arrays in property lists must also contain only property values. 我正在考虑使用这个:

            - (void)encodeWithCoder:(NSCoder *)encoder {
                //Encode properties, other class variables, etc
                [encoder encodeObject:self.selectedPhotos forKey:@"selectedPhotos"];
            }

            - (id)initWithCoder:(NSCoder *)decoder {
                if((self = [super init])) {
                    //decode properties, other class vars
                    self.selectedPhotos = [decoder decodeObjectForKey:@"selectedPhotos"];
                }
                return self;
            }

然后使用以下代码保存并检索它:

            - (void)saveCustomObject:(MyCustomObject *)obj {
                NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:obj];
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                [defaults setObject:myEncodedObject forKey:@"myEncodedObjectKey"];
            }

            - (MyCustomObject *)loadCustomObjectWithKey:(NSString *)key {
                NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
                NSData *myEncodedObject = [defaults objectForKey:key];
                MyCustomObject *obj = (MyCustomObject *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
                return obj;
            }

但我不知何故不太明白,仍然在我的代码中崩溃。不知道如何。而且我无法将其保存在 NSUserDefaults 中。希望有人帮忙。确实有一段时间有这个问题。希望有人指导我正确的保存和检索它的路径NSUserDefaults。然后回到一个NSMutableArray.

4

3 回答 3

3

唯一将NSUserDefaults一组受限制的类作为对象。请参阅文档。您必须注意仅在字典中存储这些类型的值(NSData、NSString、NSNumber、NSDate、NSArray 或 NSDictionary,当然它以递归方式应用)。

要将 URL 存储在 中NSUserDefaults,请将它们存储为字符串,然后将它们作为 URL 读回。如果您需要当前格式的字典,您可能需要在保存之前对其进行转换。

- (void) saveMyUrls
{
    NSMutableArray* urls = [NSMutableArray arrayWithCapacity:self.myUrls.count];
    for(NSURL* url in self.myUrls) {
        [urls addObject:[url absoluteString]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:urls forKey:@"myUrls"];
}


- (void) loadUrls
{
    NSArray* urls = [[NSUserDefaults standardUserDefaults] objectForKey:@"myUrls"];
    self.myUrls = [NSMutableArray arrayWithCapacity:urls.count];
    for(NSString* urlString in urls) {
        [self.myUrls addObject:[NSURL URLWithString:urlString]];
    }
    [[NSUserDefaults standardUserDefaults] setObject:urls forKey:@"myUrls"];
}

如果您需要保存比 URL 更多的信息,比如用户指定的标签,您可以将对象另存为 a NSDictionary,例如

- (void) saveMyUrlsWithLabels
{
    NSMutableArray* objs = [NSMutableArray arrayWithCapacity:self.myObjects.count];
    for(MyObject* obj in self.myObjects) {
        [objs addObject:[NSDictionary dictionaryWithKeys:@"url", @"label"
            forObjects:obj.url.absoluteString, obj.userSpecifiedLabel];
    }
    [[NSUserDefaults standardUserDefaults] setObject:objs forKey:@"myObjects"];
}
于 2012-10-16T11:32:35.547 回答
1

也许你应该这样做:

- (MyCustomObject *)loadCustomObjectWithKey:(NSString *)key {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    [defaults synchronize]; // note this
    NSData *myEncodedObject = [defaults objectForKey:key];
    MyCustomObject *obj = nil;

    // it would be even better
    // to wrap this into @try-@catch block
    if(myEncodedObject)
    {
        obj = (MyCustomObject *)[NSKeyedUnarchiver unarchiveObjectWithData: myEncodedObject];
    }

    return obj;
}

另请注意,如果您想使用NSKeyedArchiver并且NSKeyedUNarchiver您的MyCustomObject类必须符合NSCoding协议。检查NSCoding 协议参考Archives and Serializations Programming Guide

于 2012-10-16T11:40:07.753 回答
1

这是另一种方法,是的,您可以使用 NSUserDefaults。基本上你得到资产 URL,保存它,然后将它转换回资产/图像

//SET IT
ALAsset *asset3 = [self.assets objectAtIndex:[indexPath row]];
    NSMutableString *testStr = [NSMutableString stringWithFormat:@"%@", asset3.defaultRepresentation.url];

    //NSLog(@"testStr: %@ ...", testStr);

    [[NSUserDefaults standardUserDefaults] setObject:testStr forKey:@"userPhotoAsset"];
    [[NSUserDefaults standardUserDefaults] synchronize];


//GET IT 
NSString *assetUrlStr = [[NSUserDefaults standardUserDefaults] objectForKey:@"userPhotoAsset"];
    NSURL* aURL = [NSURL URLWithString:assetUrlStr];
    NSLog(@"aURL: %@ ...", aURL);

    ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    [library assetForURL:aURL resultBlock:^(ALAsset *asset)
     {
         UIImage  *copyOfOriginalImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage] scale:1.0 orientation:UIImageOrientationUp];

         imgVwPortrait.image = copyOfOriginalImage;

     }
            failureBlock:^(NSError *error)
     {
         // error handling
         NSLog(@"failure-----");
     }];
于 2014-03-04T01:01:58.290 回答