gpt4 book ai didi

适用于 iphone 5 和 iphone 4 的 UIImageview 程序 mily 管理器

转载 作者:行者123 更新时间:2023-12-03 03:27:19 25 4
gpt4 key购买 nike

enter image description here我有一个关于 UIImageView 通过 XIB 文件管理 iphone5 屏幕高度和 Iphone4 屏幕高度的问题。

我尝试像这样管理 UIImageView 的代码

~

 CGFloat screenHeight = [UIScreen mainScreen].bounds.size.height;
if ([UIScreen mainScreen].scale == 2.f && screenHeight == 568.0f) {
backgroundImage.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
frameView.autoresizingMask=UIViewAutoresizingFlexibleHeight;


backgroundImage.image = [UIImage imageNamed:@"bg-568h@2x.png"];
//frameView.frame=CGRectMake(16, 0, 288, 527);

frameView.image = [UIImage imageNamed:@"setframe-568h@2x.png"];
}
else
{
backgroundImage.image = [UIImage imageNamed:@"bg@2x.png"];
frameView.image = [UIImage imageNamed:@"setframe@2x.png"];
} ;

请向我提出有关问题的建议,FrameView 是一个具有白色图像的 UIImageView,

请谢谢

最佳答案

我遇到了同样的问题,下面是我为使其适合我所做的事情。

我在几个应用程序中使用了一些图像,需要调整大小以适应新的 4 英寸显示屏。我编写了下面的代码来根据需要自动调整图像大小,而无需指定 View 的高度。此代码假设给定图像的高度在 NIB 中调整为给定帧的完整高度,就像它是填充整个 View 的背景图像一样。在 NIB 中,UIImageView 不应设置为拉伸(stretch),这会为您拉伸(stretch)图像并扭曲图像,因为只有高度发生变化,而宽度保持不变。您需要做的是将高度和宽度调整相同的增量,然后将图像向左移动相同的增量以再次居中。这会在两侧切掉一点,同时使其扩展到给定框架的整个高度。

我这样调用它......

[self resizeImageView:self.backgroundImageView intoFrame:self.view.frame];

如果图像设置在 NIB 中,我通常会在 viewDidLoad 中执行此操作。但我也有在运行时下载并以这种方式显示的图像。这些图像是使用 EGOCache 缓存的,因此我必须在将缓存图像设置到 UIImageView 后或在下载图像并将其设置到 UIImageView 后调用调整大小方法。

下面的代码并不特别关心显示器的高度是多少。它实际上可以适用于任何显示尺寸,也许还可以处理调整图像大小以进行旋转,因为它假设每次高度的变化都大于原始高度。为了支持更大的宽度,需要调整此代码以响应该场景。

- (void)resizeImageView:(UIImageView *)imageView intoFrame:(CGRect)frame {
// resizing is not needed if the height is already the same
if (frame.size.height == imageView.frame.size.height) {
return;
}

CGFloat delta = frame.size.height / imageView.frame.size.height;
CGFloat newWidth = imageView.frame.size.width * delta;
CGFloat newHeight = imageView.frame.size.height * delta;
CGSize newSize = CGSizeMake(newWidth, newHeight);
CGFloat newX = (imageView.frame.size.width - newWidth) / 2; // recenter image with broader width
CGRect imageViewFrame = imageView.frame;
imageViewFrame.size.width = newWidth;
imageViewFrame.size.height = newHeight;
imageViewFrame.origin.x = newX;
imageView.frame = imageViewFrame;

// now resize the image
assert(imageView.image != nil);
imageView.image = [self imageWithImage:imageView.image scaledToSize:newSize];
}

- (UIImage *)imageWithImage:(UIImage *)image scaledToSize:(CGSize)newSize {
UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return newImage;
}

关于适用于 iphone 5 和 iphone 4 的 UIImageview 程序 mily 管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12648129/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com