Convert nsdictionary to nsdata

后端 未结 5 2071
伪装坚强ぢ
伪装坚强ぢ 2021-02-05 19:38

have an app that can take a picture and then upload to a server. encoding it to base 64 and pass it thru a XMLRPC to my php server.

i want to take the NSDictionary info

相关标签:
5条回答
  • 2021-02-05 20:09

    You can use an NSKeyedArchiver to serialize your NSDictionary to an NSData object. Note that all the objects in the dictionary will have to be serializable (implement NSCoding at some point in their inheritance tree) in order for this to work.

    Too lazy to go through my projects to lift code, so here is some from the Internet:

    Encode

    NSMutableData *data = [[NSMutableData alloc] init];
    NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
    [archiver encodeObject:yourDictionary forKey:@"Some Key Value"];
    [archiver finishEncoding];
    [archiver release];
    /** data is ready now, and you can use it **/
    [data release];
    

    Decode:

    NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]];
    NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
    NSDictionary *myDictionary = [[unarchiver decodeObjectForKey:@"Some Key Value"] retain];
    [unarchiver finishDecoding];
    [unarchiver release];
    [data release];
    
    0 讨论(0)
  • 2021-02-05 20:09

    I know a bit too late, but just in case someone bumps into this same issue. UIImage is not serializable, but you can serialize it using the code:

    if your image is JPG:

    NSData *imagenBinaria = [NSData dataWithData:UIImageJPEGRepresentation(imagen, 0.0)]; 
    
    // imagen is a UIImage object
    

    if your image is PNG:

    NSData *imagenBinaria = [NSData dataWithData:UIImagePNGRepresentation(imagen)]; 
    
    // imagen is a  UIImage object
    
    0 讨论(0)
  • 2021-02-05 20:24

    Three options occur to me on this, two mentioned in other answers NSKeyedArchiver and PropertyList, there is also NSJSONSerialization that gave me the most compact data in a simple test.

    NSDictionary *dictionary = @{@"message":@"Message from a cool guy", @"flag":@1};
    NSData *prettyJson = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
    NSData *compactJson = [NSJSONSerialization dataWithJSONObject:dictionary options:0 error:nil];
    NSData *plist = [NSPropertyListSerialization dataWithPropertyList:dictionary
                                                               format:NSPropertyListBinaryFormat_v1_0
                                                              options:0
                                                                error:NULL];
    NSData *archived = [NSKeyedArchiver archivedDataWithRootObject:dictionary];`
    

    Size results for the different approaches smallest to largest

    • compactJson 46 bytes
    • prettyJson 57 bytes
    • plist 91 bytes
    • archived 316 bytes
    0 讨论(0)
  • 2021-02-05 20:26

    NSDictionary -> NSData:

        NSData *myData = [NSKeyedArchiver archivedDataWithRootObject:myDictionary];
    

    NSData -> NSDictionary:

        NSDictionary *myDictionary = (NSDictionary*) [NSKeyedUnarchiver unarchiveObjectWithData:myData];
    
    0 讨论(0)
  • 2021-02-05 20:28

    The NSPropertyListSerialization class give you the most control over writing and reading of property lists:

    NSDictionary *dictionary = @{@"Hello" : @"World"};
    NSData *data = [NSPropertyListSerialization dataWithPropertyList:dictionary
                                                format:NSPropertyListBinaryFormat_v1_0
                                                options:0
                                                error:NULL];
    

    Read:

    NSData *data = ...
    NSPropertyListFormat *format;
    NSDictionary *dictionary = [NSPropertyListSerialization propertyListWithData:data
                                                            options:0
                                                            format:&format
                                                            error:NULL];
    
    0 讨论(0)
提交回复
热议问题