UIImageview程序可轻松管理iphone5和iphone4 [英] UIImageview programmilly manage for iphone5 and iphone4

查看:124
本文介绍了UIImageview程序可轻松管理iphone5和iphone4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于UIImageView的问题,该问题由iphone5屏幕的XIB文件管理高度和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英寸显示屏进行调整大小.我编写了以下代码,根据需要自动调整图像大小,而无需详细说明视图的高度.该代码假定在NIB中将给定图像的高度调整为给定帧的全高,就像它是填充整个视图的背景图像一样.在NIB中,不应将UIImageView设置为拉伸,这将为您完成拉伸图像并扭曲图像的工作,因为只有高度会发生变化,而宽度保持不变.您需要做的是将高度和宽度调整相同的增量,然后将图像向左移动相同的增量以再次居中.这样会在两侧切开一点,同时使其扩展到给定框架的整个高度.

我这样称呼...

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

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

下面的代码并不特别关心显示器的高度.它实际上可以与任何大小的显示器一起使用,也许还可以处理调整大小的图像以进行旋转,但前提是每次高度变化都大于原始高度时,它都可以假定.为了支持更大的宽度,还需要对该代码进行调整以响应这种情况.

- (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;
}

I have an Issue about UIImageView Manage by XIB file for iphone5 screen Height and Iphone4 Screen Height.

I Try to Manage code for UIImageView like this

~

 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"];
    }  ;

Please suggest me about Issues, FrameView is a UIImageView which have white Image,

Please THanks

解决方案

I had the same issue and below is what I did to make it work for me.

I have images used in a couple of apps which needed to be resized for the new 4 inch display. I wrote the code below to automatically resize images as needed without specifics on the height of the view. This code assumes the height of the given image was sized in the NIB to be the full height of the given frame, like it is a background image that fills the whole view. In the NIB the UIImageView should not be set to stretch, which would do the work of stretching the image for you and distort the image since only the height changes while the width stays the same. What you need to do is adjust the height and the width by the same delta and then shift the image to the left by the same delta to center it again. This chops off a little on both sides while making it expand to the full height of the given frame.

I call it this way...

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

I do this in viewDidLoad normally if the image is set in the NIB. But I also have images which are downloaded at runtime and displayed that way. These images are cached with EGOCache, so I have to call the resize method either after setting the cached image into the UIImageView or after the image is downloaded and set into the UIImageView.

The code below does not specifically care what the height of the display is. It actually could work with any display size, perhaps to handle resizing images for rotation as well, thought it assumes each time the change in height is greater than the original height. To support a greater width this code would need to be adjusted to respond to that scenario as well.

- (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;
}

这篇关于UIImageview程序可轻松管理iphone5和iphone4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆