上传 和 NSJsonSerialization

头像。
1.从相册or照相获取图片
2.把图片保存到沙盒Document。把图片转成二进制数据流上传到服务器。
3.将Document中的图片路径存到偏好设置。(使用user_id来标识不同的头像路径)

//从相册中选取图片或拍照  
- (void)btnActionForEditPortrait:(id) sender {  
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];  
    picker.delegate = self;  
    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;  
    picker.allowsEditing = YES;  
    [self presentViewController:picker animated:YES completion:NULL];  
}  
  
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {  
      
    _avatar = info[UIImagePickerControllerOriginalImage];  
  
    [self saveImage:_avatar WithName:@"userAvatar"];  
  
    //处理完毕,回到个人信息页面  
    [picker dismissViewControllerAnimated:YES completion:NULL];  
    [_tableView reloadData];  
} 

//保存图片  
- (void)saveImage:(UIImage *)tempImage WithName:(NSString *)imageName  
{  
    NSData* imageData = UIImagePNGRepresentation(tempImage);  
    NSString* documentPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];  
    NSString* totalPath = [documentPath stringByAppendingPathComponent:imageName];  
      
    //保存到 document  
    [imageData writeToFile:totalPath atomically:NO];  
      
    //保存到 NSUserDefaults  
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];  
    [userDefaults setObject:totalPath forKey:@"avatar"];  
      
    //上传服务器  
    [[HSLoginClass new] uploadAvatar:totalPath];  
}  
  
//从document取得图片  
- (UIImage *)getImage:(NSString *)urlStr  
{  
    return [UIImage imageWithContentsOfFile:urlStr];  
} 
上传 和 NSJsonSerialization_第1张图片
Paste_Image.png
上传 和 NSJsonSerialization_第2张图片
Paste_Image.png

----------------------NSJsonSerialization

json转为OC

-(void) loadJsonData:(NSURL *)url  
{  
    dispatch_async(kGlobalQueue, ^{  
        NSData *data = [NSData dataWithContentsOfURL:url];  
        [self performSelectorOnMainThread:@selector(parseJsonData:) withObject:data waitUntilDone:NO];  
    });  
}  
-(void) parseJsonData:(NSData *)data  
{  
    NSError *error;  
    NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];  
    if (json == nil) {  
        NSLog(@"json parse failed \r\n");  
        return;  
    }  
    NSArray *songArray = [json objectForKey:@"song"];  
    NSLog(@"song collection: %@\r\n",songArray);  
  
    _song = songArray;  
    self.songIndex = 0;  
    NSDictionary *song = [songArray objectAtIndex:0];  
    NSLog(@"song info: %@\t\n",song);  
} 

json(字典、数组) -- > 二进制流数据(json)

NSDictionary *song = [NSDictionary dictionaryWithObjectsAndKeys:@"i can fly",@"title",@"4012",@"length",@"Tom",@"Singer", nil];  
    if ([NSJSONSerialization isValidJSONObject:song])  
    {  
        NSError *error;  
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:song options:NSJSONWritingPrettyPrinted error:&error];  
        NSString *json =[[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];  
        NSLog(@"json data:%@",json);  
    }  

转载自http://blog.csdn.net/worldzhy/article/details/41575843

你可能感兴趣的:(上传 和 NSJsonSerialization)