[英]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.