上传 和 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];  
} 
Paste_Image.png
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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,824评论 4 61
  • NSURLSession基本使用 简介 使用步骤使用NSURLSession会话对象创建Task,然后执行Task...
    彼岸的黑色曼陀罗阅读 4,655评论 0 3
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,282评论 19 139
  • flex是flexible的缩写,意思为弹性布局 flex布局出现,vertical-align,float,cl...
    Lemon不怕酸啊阅读 2,224评论 0 0
  • 第一次写“简书”,深夜了,刚结束一群人的狂欢,然后带着一身的孤单感回到家,我好想你,在今天第一次对你发脾气之后,可...
    Amaya小姐姐阅读 1,895评论 0 0

友情链接更多精彩内容