1

我真的很陌生,如果這是一個非常簡單的問題,我很抱歉。我試圖編寫一個代碼來使用UIImagePickerController從我的圖像庫中選擇兩個不同的圖像,並將其放入兩個UIImageView s。然後,點擊一個按鈕將圖像上傳到Parse。對兩個ImageViews使用UIImagePickerController並上傳到解析

我有困難使用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

爲了區分照片A和照片B,您可以使用BOOL狀態屬性,或者驗證imageViewA.image!= nil是否傳遞給imageViewB。它應該做到您choosePhotoA方法集中所有 –

+0

非常感謝你的幫助! – user3193423

回答

0

當圖像被拾取並didFinishPickingMediaWithInfo:被調用時,你需要知道哪些圖像被攝像(1或2)。要做到這一點,按下按鈕時保存一個標誌,調用choosePhotoA:。然後,在didFinishPickingMediaWithInfo:中,您可以使用該標誌來決定放置圖像的位置(imageViewAimageViewB)。這將允許用戶改變主意並替換他們已經選擇的圖像。

如果需要,您還可以將圖像保存到磁盤,或使用@property s來保存對它們的引用(您並不真正想要從圖像視圖返回圖像)。

現在,當你想上傳,獲取圖像(從屬性,圖像視圖或磁盤)和上傳(基本上這只是改變你當前的使用UIImage imageNamed:)。你需要運行代碼兩次來處理每個圖像。

+0

你真了不起。非常感謝你的幫助!! – user3193423