2017-04-18 84 views
0

我正在開發一個應用程序,它有兩個圖像視圖。每個圖像視圖都有自己的按鈕來從相機膠捲中選擇圖像。當我點擊一個按鈕時,它會顯示一個相機膠捲,我選擇一個圖像並顯示在圖像視圖1中,但是當我單擊第二個按鈕從相機膠捲選擇圖像並選擇圖像時,它會在圖像view1上顯示圖像的imageview2。有誰知道如何解決它???這是我的界面。我正在使用Objective-c語言。創建未知類型的圖像格式是錯誤(lldb)

enter image description here

這裏是代碼:

- (IBAction)CNICFront:(id)sender { 
    picker = [[UIImagePickerController alloc]init]; 
    picker.delegate=self; 
    [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]‌​; 
    [self presentViewController:picker animated:YES completion:NULL]; 
} 

- (IBAction)CNICBack:(id)sender { 
    pic = [[UIImagePickerController alloc]init]; 
    pic.delegate=self; 
    pic setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]‌​; 
    [self presentViewController:pic animated:YES completion:NULL]; 
} 

委託方法可用於圖像視圖1是這樣的: -

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 
    image=[info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
    [self.imageView1 setImage:image]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

-(void)imagePickerControllerDidCancel:(UIImagePickerControll‌​er *)picker{ 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 
+0

假設u有2圖像視圖v1和v2 ......在第二回合按鈕動作U設置定圖像v1.image =您的圖像更改爲v2.image =您的圖像。或(從我們的屏幕鏡頭)我認爲你給予同樣的行動2按鈕相同,在這種情況下,你需要使用標籤值你的Takeapic按鈕 –

+1

後您的代碼很容易解決您的問題。 –

+0

- (IBAction)CNICFront:(id)sender {picker = [[UIImagePickerController alloc] init]; picker.delegate = self; [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; [self presentViewController:picker animated:YES completion:NULL]; (IBAction)CNICBack:(id)發送者{= {[UIImagePickerController alloc] init]; pic.delegate = self; [pic setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; [self presentViewController:pic animated:YES completion:NULL]; } – Hamza

回答

0

enter image description here更新代碼:例如

.h文件中:

@property (weak, nonatomic) IBOutlet UIImageView *img1; 
@property (weak, nonatomic) IBOutlet UIImageView *img2; 
- (IBAction)but1:(UIButton *)sender; 
- (IBAction)but2:(UIButton *)sender; 

.m文件文件

#import "ViewController.h" 

    @interface ViewController()<UIImagePickerControllerDelegate,UINavigationControllerDelegate>{ 
     UIImagePickerController *picker; 
     NSString *ImageViewC; 
    } 

    @end 

    @implementation ViewController 

    - (void)viewDidLoad { 
     [super viewDidLoad]; 
    picker = [[UIImagePickerController alloc]init]; 
    picker.delegate=self; 


    } 


    - (void)didReceiveMemoryWarning { 
     [super didReceiveMemoryWarning]; 
     // Dispose of any resources that can be recreated. 
    } 


    - (IBAction)but1:(UIButton *)sender { 
     [email protected]"1"; 

     [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     [self presentViewController:picker animated:YES completion:NULL]; 

    } 

    - (IBAction)but2:(UIButton *)sender { 
     [email protected]"2"; 

     [picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
     [self presentViewController:picker animated:YES completion:NULL]; 

    } 

    -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{ 

     UIImage * image=[info objectForKey:@"UIImagePickerControllerOriginalImage"]; 
     if ([ImageViewC isEqualToString:@"1"]) { 
      [self.img1 setImage:image]; 
     }else{ 
      [self.img2 setImage:image]; 
     } 
     [self dismissViewControllerAnimated:YES completion:NULL]; 
    } 

    -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{ 
     [self dismissViewControllerAnimated:YES completion:NULL]; 

    } 
    @end 
+0

此代碼在imageView2中顯示圖像。實際上是,如果我點擊button1它應該在imageView1中顯示圖像,如果我點擊button2它應該顯示圖像在ImageView 2 @Naveen庫馬爾 – Hamza

+0

viewdidload Intilaze ImageViewC字符串像ImageViewC = [[NSString alloc]初始化];並請檢查我更新的代碼...以指定ImageViewC的值。 –

+0

這是一樣的,當我點擊button1它在imageView2中顯示圖像,當點擊button2並選擇圖像時,它在imageView2中顯示圖像 – Hamza