简体   繁体   English

如何在traitCollectionDidChange的视图控制器中更改UIImageView图像

[英]How do I change UIImageView image in view controller in traitCollectionDidChange

I want the image in my UIImageView to us a different image file that's cropped to correctly fill the landscape mode upon the orientation changing from portrait to landscape. 我希望将UIImageView中的图像提供给我们一个不同的图像文件,该图像文件在方向从纵向更改为横向时,可以裁剪为正确填充横向模式。 All of the following code is defined in a class that extends UIViewController . 以下所有代码均在扩展UIViewController的类中定义。

The following function is definitely being called when the phone rotates because my printf function is printing to the output. 旋转手机时肯定会调用以下函数,因为我的printf函数正在打印至输出。

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {

    [super traitCollectionDidChange: previousTraitCollection];
    if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
        || (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {
        printf("Orientation Change!\n");
        UIImage * newImage = [UIImage imageNamed: @"landscape-image"];
        [self.imageView setImage:newImage];
    }
}

I defined the property imageView in my .h file as follows: 我在.h文件中定义了属性imageView ,如下所示:

@property (nonatomic, nonnull, readonly) UIImageView * imageView;

And I initialize the imageView as follows: 我将imageView初始化如下:

UIImage * image = [UIImage imageNamed:@"portrait-image"];
UIImageView * _imageView = [[UIImageView alloc] initWithImage:image];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
_imageView.translatesAutoresizingMaskIntoConstraints = NO;
[pageView addSubview:_imageView];

This does not work for me though. 不过这对我不起作用。 When I change the orientation, the imageView image stays the same and zooms in like it normally does. 当我改变方向时,imageView图像保持不变,并且像往常一样放大。 Since the traitCollectionDidChange function is being called when the phone rotates, I assume the issue must be with how I'm changing the image. 由于手机旋转时会调用traitCollectionDidChange函数,因此我认为问题一定与如何更改图像有关。 I'm relatively new to iOS development so I could just be missing something important for updating UIImageViews. 我是iOS开发的新手,所以我可能会缺少更新UIImageViews的重要内容。 Any help is appreciated. 任何帮助表示赞赏。

When you create the imageView using UIImageView * _imageView = [[UIImageView alloc] initWithImage:image]; 使用UIImageView * _imageView = [[UIImageView alloc] initWithImage:image];创建UIImageView * _imageView = [[UIImageView alloc] initWithImage:image]; , you are shadowing the automatically synthesised variable _imageView, with a new variable of the same name. ,您将使用相同名称的新变量来遮盖自动合成的变量_imageView。

The newly created UIImageView instance is therefore not assigned to the imageView property. 因此,新创建的UIImageView实例未分配给imageView属性。 As a result, when the device is rotated and the second method is run, self.imageView is nil, and you call to [self.imageView setImage:newImage] does nothing. 结果,当旋转设备并运行第二种方法时,self.imageView为nil,并且您调用[self.imageView setImage:newImage]不会执行任何操作。

To solve it, all you need to do replace UIImageView * _imageView = [[UIImageView alloc] initWithImage:image]; 为了解决这个问题,您需要做的只是替换UIImageView * _imageView = [[UIImageView alloc] initWithImage:image]; with _imageView = [[UIImageView alloc] initWithImage:image]; _imageView = [[UIImageView alloc] initWithImage:image];

More on the messiness of automatically synthesised properties in Objective-C in this answer When should I use @synthesize explicitly? 有关此答案中Objective-C中自动合成属性的混乱情况, 何时应该显式使用@synthesize?

EDIT -- 编辑-

In addition, the - (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection may be called at any time, and initially, so you want to detect the orientation in that method, and not just set the image to your landscape image. 另外, - (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection可能会在任何时候被调用,并且最初会被调用,因此您想要检测该方法的方向,而不仅仅是将图像设置为风景图像。

(and on this - ou may want to re-evaluate your requirements regarding 'landscape' vs 'portrait' image because iOS apps can run in a variety of environment, iPad / iPhone / compact.. it is complicated. https://developer.apple.com/documentation/uikit/uitraitcollection ) (在此基础上-您可能要重新评估您对“风景”与“人像”图像的要求,因为iOS应用可以在各种环境(iPad / iPhone /紧凑型环境中运行)。这很复杂。https:// developer .apple.com / documentation / uikit / uitraitcollection

To complete the answer, this should work on iPhone 要完成答案,这应该可以在iPhone上使用

- (void) traitCollectionDidChange: (UITraitCollection *) previousTraitCollection {

    [super traitCollectionDidChange: previousTraitCollection];

    if ((self.traitCollection.verticalSizeClass != previousTraitCollection.verticalSizeClass)
        || (self.traitCollection.horizontalSizeClass != previousTraitCollection.horizontalSizeClass)) {

         if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassCompact) {
              [self.imageView setImage:[UIImage imageNamed: @"landscape-image"]];
         } else if (self.traitCollection.verticalSizeClass == UIUserInterfaceSizeClassRegular
                    && self.traitCollection.horizontalSizeClass == UIUserInterfaceSizeClassCompact) {
             [self.imageView setImage:[UIImage imageNamed: @"portrait-image"]];
         }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何更改为另一个视图控制器 - How do I change to another view controller 如何同时为UIImageView的图像和UIImageView本身设置动画? - How do I simultaneously animate a UIImageView's image and the UIImageView itself? 如何直接将图像分配给UIImageView? - How do I directly assign an image to a UIImageView? 从图像选择器返回时如何更改视图控制器? - How do I change view controller when returning from image picker? 如何在从图片库拍摄的uiimageview中显示拾取的图像到另一个视图控制器中的另一个uiimageview中? - How to display picked image in a uiimageview taken from the photo library to another uiimageview in a different view controller? 重新加载View Controller并以编程方式更改UIImageView约束 - Reload View Controller and programatically change UIImageView constraint IBAction在第二个View Controller中设置UIImageview图像? - IBAction to set UIImageview image in second View Controller? 如何使用ex更改UIImageView的图像 [触摸视图] - How to change image of UIImageView using ex. [touch view] 将可选图像 UIImageView 传递给另一个视图控制器 - pass an optional image UIImageView to another view controller 在“中心”模式下,如何在 UIImageView 中缩放图像? (换帧不起作用) - how do I scale the image in a UIImageView when in “centre” mode? (change frame doesn't work)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM