我正在创建一个练习地址簿应用程序,我想添加用户上传他的图片的功能。我在网上搜索过,但是找不到有关如何实现这一目标的有用教程。
如果有人知道上传图片的好教程,请分享。
还有一件事,如果用户上传他/她的图像,我希望图像也存储在核心数据中,以便它可以显示在表视图控制器和用户可以上传的更新视图控制器中新图像并替换上一图像。
任何解释此问题的好教程都会有所帮助。
提前致谢
答案 0 :(得分:1)
首先请在进行任何问题之前做一些研究。您需要使用UImagePickerController,如下所示:
您需要将这些代理添加到viewController
UIActionSheetDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate.
一旦您希望用户选择图像
[[[UIActionSheet alloc] initWithTitle:@"Choose media type"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:nil
otherButtonTitles:@"Camera", @"Library", nil] showInView:self.view];
这将显示UIActionSheet,以便用户可以从相机或库中进行选择
用户选择其中一个选项后,将调用UIActionSheet委托方法
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
UIImagePickerController *picker = [[UIImagePickerController alloc] init];
picker.delegate = self;
picker.allowsEditing = YES;
switch (buttonIndex) {
case 0:
picker.sourceType = UIImagePickerControllerSourceTypeCamera;
break;
case 1:
picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
break;
}
[self presentViewController:picker animated:YES completion:NULL];
}
在这里,您可以选择以太网添加相机或添加库
现在,在用户选择照片(库/相机)之后,将调用UIImagePickerControllerDelegate方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
}
chosenImage是用户从UIImagePickerController中选择的图像。
答案 1 :(得分:0)
我建议您使用AFNetworking
NSString *BASE_URL = ... //Your server base url
AFHTTPRequestOperationManager *operationManager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:BASE_URL]];
NSString *path = ... //Your relative path
NSDictionary *params = @{};//Your params
[operationManager POST:@"" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
if (mediaFile) {
[formData appendPartWithFileData:mediaFile name:@"name" fileName:@"fileName" mimeType:@"image/jpeg"];
}
} success:^(AFHTTPRequestOperation *operation, id responseObject) {
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
}];