iOS 图像延伸到应用程序之外
我的 UIViewController 中有一个 UIImageView 。它加载一个 81x37、原点为 0x20 的图像。我想做的是创建该图像作为我的背景和徽标。我的意思是,当我启动我的应用程序时,我希望图像包含整个屏幕,一旦我加载了所有数据并准备好向用户显示,我想将其缩小为只是一个图标屏幕的左上角。我想说的是,调整大小的代码是有效的,但是它似乎拉伸了图像并将其切断了大约一半。因此,它要么在屏幕尺寸之外渲染,要么只渲染它能渲染的部分。
下面是我如何处理帧大小调整的代码片段,这是在 viewDidLoad 和 isReady 函数中完成的。
- (void) viewDidLoad
{
originalSize = CGRectMake(self.logoImage.frame.origin.x, self.logoImage.frame.origin.y, self.logoImage.frame.size.width, self.logoImage.frame.size.height);
fullScreenSize = CGRectMake(0.0, 0.0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[self.logoImage setFrame:fullScreenSize];
}
- (void)isReady:(BOOL)initialized
{
if(initialized)
{
[self.logoImage setFrame:self.originalSize];
}
else
{
[self.logoImage setFrame:self.fullScreenSize];
}
}
I have a UIImageView inside of my UIViewController. It loads an image that is 81x37 with an origin of 0x20. What I am trying to do is create that image as my background as well as a logo. What I mean by that is when I start my application I would like the image to encompass the whole screen and once I have all my data loaded and ready to display out to the user I would like to shrink it to just be an icon at the upper left hand side of the screen. I would like to say that the code for resizing works, however it seems to stretch the image and cut it off about half way. So either it is rendering outside the dimensions of the screen or only renders what it can.
Below is a code snippet of how I handle my frame resizing which is done in both my viewDidLoad and my isReady functions.
- (void) viewDidLoad
{
originalSize = CGRectMake(self.logoImage.frame.origin.x, self.logoImage.frame.origin.y, self.logoImage.frame.size.width, self.logoImage.frame.size.height);
fullScreenSize = CGRectMake(0.0, 0.0, [[UIScreen mainScreen] bounds].size.width, [[UIScreen mainScreen] bounds].size.height);
[self.logoImage setFrame:fullScreenSize];
}
- (void)isReady:(BOOL)initialized
{
if(initialized)
{
[self.logoImage setFrame:self.originalSize];
}
else
{
[self.logoImage setFrame:self.fullScreenSize];
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你为什么不直接设置originalSize = self.logoImage.frame和fullScreenSize = [[UIScreen mainScreen]bounds]?
无论如何,假设您的 logoImage 是一个 imageView,您是否将 imageView 的 contentMode 设置为 UIViewContentModeAspectFit 或等效值?默认情况下,imageViews 不会缩放图像。
Why don't you just set originalSize = self.logoImage.frame and fullScreenSize = [[UIScreen mainScreen] bounds]?
Anyway, assuming your logoImage is an imageView, have you set the contentMode of the imageView to UIViewContentModeAspectFit, or equivalent? By default imageViews don't scale the image otherwise.
感谢达摩回答我的问题。答案就在评论里。
I would like to thank Damo for answering my question. The answer is in the comments.