2013-10-27 44 views
0

請幫助推送在UIImageView框架中添加的照片。照片不適合框架。我在「IOS編程:大書呆子牧場指南」第12章「相機」中做出了相同的操作。添加照片之前:https://www.dropbox.com/s/o0uypvspuhvlkm2/Screen%20Shot%202013-10-27%20at%205.08.39%20PM.jpg添加之後:https://www.dropbox.com/s/u6qffb1l941cb0v/Screen%20Shot%202013-10-27%20at%205.09.02%20PM.jpg我試過setContentMode UIImageView - 不起作用。這是我的代碼:添加的照片不適合UIImageView的框架

#pragma mark UIActionSheetDelegate 

- (IBAction)displayActionSheet:(id)sender 
{ 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:nil 
                  delegate:self 
                cancelButtonTitle:@"Cancel" 
               destructiveButtonTitle:nil 
                otherButtonTitles:@"Take photo", @"Choose photo from gallery", nil]; 

    actionSheet.actionSheetStyle = UIActionSheetStyleDefault; 
    [actionSheet showInView:self.view]; 
} 

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex 
{ 
    switch (buttonIndex) { 
     case 0: 
      [self takePhoto]; 
      break; 
     case 1: 
      [self choosePhoto]; 
      break; 
    } 
} 

#pragma mark UIImagePickerControllerDelegate 

- (void)takePhoto { 
    UIImagePickerController *pickerControllerTakePhoto = [[UIImagePickerController alloc] init]; 
    [pickerControllerTakePhoto setSourceType:UIImagePickerControllerSourceTypeCamera]; 
    [pickerControllerTakePhoto setDelegate:self]; 
    [self presentViewController:pickerControllerTakePhoto animated:YES completion:nil]; 
} 

- (void)choosePhoto { 
    UIImagePickerController *pickerControllerChoosePhoto = [[UIImagePickerController alloc] init]; 
    [pickerControllerChoosePhoto setSourceType:UIImagePickerControllerSourceTypePhotoLibrary]; 
    [pickerControllerChoosePhoto setDelegate:self]; 
    [self presentViewController:pickerControllerChoosePhoto animated:YES completion:nil]; 
} 

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { 
    UIImage *myPhoto = [info objectForKey:UIImagePickerControllerOriginalImage]; 
    [photoView setImage:myPhoto]; 
    [self dismissViewControllerAnimated:YES completion:NULL]; 
} 

我「加」或「添加」與對話框按鈕調用的動作片,所以我選擇從畫廊和結果的照片就像是在第二個屏幕。

+0

是'photoView'真是的UIImageView? – SAE

+0

是的,'__weak IBOutlet UIImageView * photoView;' –

+0

Second screenshot:這是你嗎? – Mundi

回答

0

設置圖像視圖的屬性:

imageView.clipsToBounds = YES; 
imageView.contentMode = UIViewContentModeScaleAspectFill; 

兩者都可以在界面生成器被設置。

+0

不工作,具有設置屬性和添加代碼在' - (無效)imagePickerController:(UIImagePickerController *)選擇器didFinishPickingMediaWithInfo:(NSDictionary *)信息' –

0

所以我發現其中擾亂我的代碼行,因爲這是我整理我的tableview我定製單元的這一切:

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { 
    UITableViewCell *cell = nil; 
    if (indexPath.row == 0) { 
     photoView = (UIImageView *)[photoCell viewWithTag:1]; // this is the first custom cell in my tableview and this line which I put on the model of the book isn't needed 
     cell = photoCell; 
............................... 
+0

很高興你找到它。 –