iOS开发之上传图片视频到服务器
响应点击事件的方法:
这里我是用的系统的相册,没有自定义。也没用进行对应的压缩。需要的朋友需要自行解决了。但这些代码保证能给上传成功。
-(void)photoBtnSelecoed{
NSLog(@"图片");
UIImagePickerController *picker = [[UIImagePickerController alloc]init];
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
picker.delegate = self;
下句代码是系统相册里图片跟视频可以共同显示出来
picker.mediaTypes = @[(NSString *)kUTTypeMovie,(NSString *)kUTTypeImage];
picker.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
picker.allowsEditing = YES;
[self presentViewController:picker animated:YES completion:nil];
}
具体获取选择的图片或视频进行上传
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
[picker dismissViewControllerAnimated:YES completion:nil];
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
manager.requestSerializer = [AFHTTPRequestSerializer serializer];
manager.responseSerializer = [AFHTTPResponseSerializer serializer];
NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:@"public.image"]) {
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:self.access_key forKey:@"access_key"];
[parameters setObject:@"12" forKey:@"receive_u_id"];
[parameters setObject:@"2" forKey:@"msg_type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json",
@"text/html",
@"image/jpeg",
@"image/png",
@"application/octet-stream",
@"text/json",
nil];
[manager POST:[NSString stringWithFormat:@"%@%@",SERVER_URL,MSG_MESSAGE_SEND] parameters:parameters constructingBodyWithBlock:^(id_Nonnull formData) {
UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];
NSData *data = UIImagePNGRepresentation(image);
//上传的参数(上传图片,以文件流的格式)
[formData appendPartWithFileData:data
name:@"file"
fileName:@"gauge.png"
mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"上传成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败%@",error);
}];
dispatch_async(dispatch_get_main_queue(), ^{
上传后立即刷新数据在主线程里
[self requestOtherMessageList];
});
} else if ([mediaType isEqualToString:@"public.movie"]){
NSLog(@"选择的视频");
NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
[parameters setObject:self.access_key forKey:@"access_key"];
[parameters setObject:@"12" forKey:@"receive_u_id"];
[parameters setObject:@"3" forKey:@"msg_type"];
manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/html",
@"image/jpeg",
@"image/png",
@"application/octet-stream",
@"text/json",
nil];
[manager POST:[NSString stringWithFormat:@"%@%@",SERVER_URL,MSG_MESSAGE_SEND] parameters:parameters constructingBodyWithBlock:^(id_Nonnull formData) {
NSURL *videoURL = [info objectForKey:UIImagePickerControllerMediaURL];
NSLog(@"%@",videoURL);
NSData *videoData = [NSData dataWithContentsOfURL:videoURL];
//上传的参数(上传图片,以文件流的格式)
[formData appendPartWithFileData:videoData
name:@"file"
fileName:@"gauge.png"
mimeType:@"image/png"];
} progress:^(NSProgress * _Nonnull uploadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"上传成功");
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
NSLog(@"上传失败%@",error);
}];
dispatch_async(dispatch_get_main_queue(), ^{
立即在主线程里刷新数据
[self requestOtherMessageList];
});
}