我是编程新手,如果这是一个非常简单的问题,我会提前道歉。我正在尝试编写代码以使用UIImagePickerController
从我的imageGallery中选择两个不同的图像并将其放入两个UIImageView
中。然后,单击按钮将图像上载到Parse。
我在使用UIImagePickerController
两次时遇到困难,似乎无法在任何地方找到答案。此外,我无法将其链接到上传代码。我只学会了如何上传文件夹中已有的文件,但我似乎无法弄清楚如何将其链接到UIImagePickerController
。如果你能提供帮助,我们将非常感激。我的代码到目前为止......
- (IBAction)choosePhotoA:(id)sender {
UIImagePickerController *imagePickerControllerA = [[UIImagePickerController alloc] init];
imagePickerControllerA.modalPresentationStyle = UIModalPresentationCurrentContext;
imagePickerControllerA.delegate =self;
imagePickerControllerA.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
[self presentViewController:imagePickerControllerA animated:NO completion:nil];
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
[self dismissViewControllerAnimated:YES completion:nil];
}
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
UIImage *imageA = [info valueForKey:UIImagePickerControllerOriginalImage];
self.imageViewA.image = imageA;
[self dismissViewControllerAnimated:YES completion:nil];
}
- (IBAction)uploadPhotoA:(id)sender {
PFObject *newImage = [PFObject objectWithClassName:@"photos"];
NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"newImage.png"]);
PFFile *newImageFile = [PFFile fileWithName:@"testPhoto.png" data:imageData];
[newImage setObject:newImageFile forKey:@"photoUploadOne"];
[newImage saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error){
NSLog(@"upload success!");
}
}];
}
答案 0 :(得分:0)
选择图像并调用didFinishPickingMediaWithInfo:
时,您需要知道正在拾取的图像(1或2)。要执行此操作,请在按下按钮时保存标记,并调用choosePhotoA:
。然后,在didFinishPickingMediaWithInfo:
中,您可以使用该标记来决定放置图片的位置(imageViewA
或imageViewB
)。这将允许用户改变主意并替换他们已经选择的图像。
如果您愿意,也可以将图像保存到磁盘,或者使用@property
来保存对它们的引用(您真的不想从图像视图中恢复图像)。 / p>
现在,当您想要上传时,获取图像(来自属性,图像视图或磁盘)并上传(基本上这只会改变您当前使用的UIImage imageNamed:
)。并且您需要运行代码两次以处理每个图像。