【问题标题】:How to make UIImageView correct frame size?如何使 UIImageView 正确的帧大小?
【发布时间】:2014-12-30 09:53:50
【问题描述】:

我正在尝试使用 imagePickerController 拍照并在另一个 ViewController 的 UIImageView 中显示图像。

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    [picker dismissViewControllerAnimated:YES completion:NULL];
    PAWWallPostCreateViewController *wallPostCreateViewController = [[PAWWallPostCreateViewController alloc] initWithNibName:@"PAWWallPostCreateViewController" bundle:nil];

    wallPostCreateViewController.myImage = info[UIImagePickerControllerEditedImage];
    wallPostCreateViewController.imageView = [[UIImageView alloc] initWithImage:wallPostCreateViewController.myImage];
    wallPostCreateViewController.imageView.contentMode = UIViewContentModeScaleAspectFit;

    NSLog(@"%@",wallPostCreateViewController.imageView); // Output below
    [self.navigationController presentViewController:wallPostCreateViewController animated:YES completion:nil];
}

imageView 在 NSLog 中的输出是:

<UIImageView: 0x18f695b0; frame = (0 0; 640 640); opaque = NO; userInteractionEnabled = NO; layer = <CALayer: 0x18f14240>>

框架对于我设置为 (60 227; 200 200) 的实际 UIImageView 来说太大了

我认为这就是为什么我在运行我的应用程序时根本看不到 UIImageView 的原因。我该如何解决这个问题?

编辑:

在 PAWWallPostCreateViewController.h 中

@property (nonatomic, strong) IBOutlet UIImageView *imageView;

我将 imageView 链接到我的 .xib 文件

还有谁熟悉 autoresize = RM+BM;因为通常当我使用 imagePickerController 将 UIImage 放在同一视图控制器的 UIImageView 中时,我的 UIImageView 数据具有 autoresize = RM+BM;属性 所以我想要的代码通常看起来像

<UIImageView: 0x1a2b4b30; frame = (60 227; 200 200); opaque = NO; autoresize = RM+BM; userInteractionEnabled = NO; layer = <CALayer: 0x1a2b4bb0>>

但这就是我在这种情况下得到的结果

【问题讨论】:

  • 试试 imageView.contentMode = UIViewContentModeScaleAspectFit;
  • 什么都不做。我更新了代码以显示我做了什么
  • 你能展示一下你是如何在 PAWWallPostCreateViewController 中添加 imageview 的吗?
  • 我编辑了上面的代码。我刚刚定义了@property (nonatomic, strong) IBOutlet UIImageView *imageView;在我的 .h 文件中并将其链接到我的 .xib 文件

标签: ios objective-c uiviewcontroller uiimageview frame


【解决方案1】:

只需在 ViewDidLoad() 中的第二个视图控制器下调用此行

self.imageView.contentMode = UIViewContentModeScaleAspectFit;

【讨论】:

  • 你有没有为 imageivew 提供任何修复框架?
  • 对不起我之前的评论。这实际上解决了问题!现在在 NSLog 中,我收到了正确的大小,但是当我运行我的应用程序时,我仍然看不到 UIImage 或 UIImageView。一定是某个地方还有其他问题......所以我得做一些搜索
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-11
  • 2017-08-10
相关资源
最近更新 更多