Skip to content

Instantly share code, notes, and snippets.

@korrio
Created January 29, 2016 16:45
Show Gist options
  • Select an option

  • Save korrio/b6558d9b2f7f569b9a03 to your computer and use it in GitHub Desktop.

Select an option

Save korrio/b6558d9b2f7f569b9a03 to your computer and use it in GitHub Desktop.
upload avatar
-(void)RequestUpdateAvatarImage:(UIImage *) image VDOMAXUserID:(int ) userid withToken:(NSString *) token UserPassword:(NSString *) pass{
NSString * urlString = [NSString stringWithFormat:@"%@/request.php?t=avatar&a=new&user_id=%d&token=%@&user_pass=%@",kIPServiceAddress,userid,token,pass];
NSString* encodedUrl = [urlString stringByAddingPercentEscapesUsingEncoding:
NSUTF8StringEncoding];
NSDictionary * params = [[NSDictionary alloc] initWithObjectsAndKeys:[NSString stringWithFormat:@"%d",userid],@"timeline_id",
nil];
NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:encodedUrl parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
NSData *imageData = UIImageJPEGRepresentation(image, 1.0);
[formData appendPartWithFileData:imageData name:@"image" fileName:[NSString stringWithFormat:@"avatar%d.jpg", userid] mimeType:@"image/jpeg"];
} error:nil];
[request setValue:token forHTTPHeaderField:@"X-Auth-Token"];
AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
NSURLSessionUploadTask *uploadTask;
uploadTask = [manager
uploadTaskWithStreamedRequest:request
progress:^(NSProgress * _Nonnull uploadProgress) {
dispatch_async(dispatch_get_main_queue(), ^{
});
}
completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) {
if (error) {
NSLog(@"Error: %@", error);
enableCallService = NO;
} else {
if ([[responseObject objectForKey:@"status"] intValue] == 200) {
[_imvProfile setImage:imageNewUpdate];
[[[UIAlertView alloc] initWithTitle:@"Update photo profile" message:@"update succss!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil] show];
// NSLog(@"%@", [resultDictionary description]);
NSString * newAvatarPath = [responseObject objectForKey:@"avatar_url"];
app.userData.avatarURL = newAvatarPath;
[[NSUserDefaults standardUserDefaults] setObject:newAvatarPath forKey:@"AVATARURL"];
[[NSUserDefaults standardUserDefaults] synchronize];
[app AppCallRefreshPhotoProfile];
}else{
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Update Avatar Image" message:@"update failed!" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
}
}];
[uploadTask resume];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment