UIImageview programmilly manage for iphone5 and iphone4

Deadly 提交于 2019-12-07 14:32:25

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/12648129/uiimageview-programmilly-manage-for-iphone5-and-iphone4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!