2011-06-20 35 views
0

我試圖創建一個應用程序,它可以讓您從照片庫導入照片並將它們插入到指定的UIImageView中。我能夠讓它在我的界面中爲其中一個UIImageView工作,但無法分配給第二個UIImageView。從照片庫導入多個圖像到應用程序

如果有人能告訴我我做錯了什麼,我將不勝感激!謝謝! SBB

這裏是我的代碼:

@implementation FlipBook2ViewController 


- (IBAction)selectExistingPicture { 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypePhotoLibrary]) { 
     UIImagePickerController *picker = 
     [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsImageEditing = NO; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [self presentModalViewController:picker animated:YES]; 
     [picker release]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"Error accessing photo library" 
           message:@"Device does not support a photo library" 
           delegate:nil 
           cancelButtonTitle:@"Dismiss" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 


- (IBAction)selectExistingPicture2 { 
    if ([UIImagePickerController isSourceTypeAvailable: 
     UIImagePickerControllerSourceTypePhotoLibrary]) { 
     UIImagePickerController *picker = 
     [[UIImagePickerController alloc] init]; 
     picker.delegate = self; 
     picker.allowsImageEditing = NO; 
     picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
     [self presentModalViewController:picker animated:YES]; 
     [picker release]; 
    } 
    else { 
     UIAlertView *alert = [[UIAlertView alloc] 
           initWithTitle:@"Error accessing photo library" 
           message:@"Device does not support a photo library" 
           delegate:nil 
           cancelButtonTitle:@"Dismiss" 
           otherButtonTitles:nil]; 
     [alert show]; 
     [alert release]; 
    } 
} 


#pragma mark - 
- (void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image 
        editingInfo:(NSDictionary *)editingInfo { 
    imageView.image = image; 
    [picker dismissModalViewControllerAnimated:YES]; 

} 
- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 

    [picker dismissModalViewControllerAnimated:YES]; 
} 

回答

0

imageView的委託方法imagePickerController:didFinishPickingImage:editingInfo:沒有改變。你或許應該有當前圖像視圖的實例變量,這樣說:currentImageView並實現委託方法類似,

- (void)imagePickerController:(UIImagePickerController *)picker 
     didFinishPickingImage:(UIImage *)image 
        editingInfo:(NSDictionary *)editingInfo { 
    currentImageView.image = image; 
    [picker dismissModalViewControllerAnimated:YES]; 

} 

對於這個然而工作,你將不得不改變你的selectExistingPicture一點。

- (IBAction)selectExistingPicture { 
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypePhotoLibrary]) { 

     currentImageView = imageView1; // Set it to the image view that will get changed 

     [..] 
    } 
    else { 
     [..] 
    } 
} 

但是,您必須爲每種方法執行此操作。我不確定此方法的確切觸發器是什麼,但在動作方法中使用sender參數以避免重複是適當的。

- (IBAction)selectExistingPicture:(id)sender { 
    switch(sender.tag) { 
     /* set `currentImageView` based on the tag */ 
    } 

    /* Rest of your method from the question remains the same */ 
} 
+0

感謝您的諮詢!我會測試它! – Sunny

+0

嗯..似乎沒有工作。我有兩個按鈕和兩個ImageViews,當按下相應的按鈕時,它們應該由照片庫中選擇的照片填充。 – Sunny

+0

你做了什麼改變? –

相關問題