【问题标题】:Added photo does not fit in the frame of UIImageView添加的照片不适合 UIImageView 的框架
【发布时间】:2013-10-27 14:27:31
【问题描述】:

请帮忙将添加的照片推送到 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 我已经为 UIImageView 尝试了setContentMode - 不起作用。这是我的代码:

#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];
}

我的“加号”或“添加”按钮调用带有对话框的操作表,所以我从图库中选择照片,结果就像第二个屏幕截图一样。

【问题讨论】:

  • photoView 真的是 UIImageView 吗?
  • 是的,__weak IBOutlet UIImageView *photoView;
  • 第二张截图:这是你吗?
  • photoView的frame是什么?
  • 如何将 photoView 添加到视图层次结构中?它的superview是什么?

标签: ios objective-c uiimageview photo xcode5


【解决方案1】:

设置图像视图属性:

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

两者都可以在 Interface Builder 中设置。

【讨论】:

  • 不起作用,在- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info中设置属性和添加代码
【解决方案2】:

所以我找到了让我感到不安的代码行,这完全是因为我将自定义单元格放置到我的 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;
...............................

【讨论】:

    猜你喜欢
    • 2012-09-08
    • 2016-11-28
    • 1970-01-01
    • 1970-01-01
    • 2012-02-12
    • 2017-08-24
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多