2012-09-28 31 views
3

enter image description here我有一個關於UIImageView管理的XIB文件爲iPhone5屏幕高度和Iphone4屏幕高度的問題。UIImageview programmilly管理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:@"[email protected]"]; 
     //frameView.frame=CGRectMake(16, 0, 288, 527); 

     frameView.image = [UIImage imageNamed:@"[email protected]"]; 
    } 
    else 
    { 
     backgroundImage.image = [UIImage imageNamed:@"[email protected]"]; 
     frameView.image = [UIImage imageNamed:@"[email protected]"]; 
    } ; 

請給我建議有關的問題,FrameView是一個UIImageView具有白色圖像,

請 感謝

回答

-1

我有同樣的問題,下面是我做的,使它爲我工作。

我有一些應用程序需要爲新的4英寸顯示屏調整大小的圖像。我編寫了下面的代碼,根據需要自動調整圖像的大小,而不必考慮視圖的高度。此代碼假定給定圖像的高度在NIB中的大小爲給定幀的全部高度,就像它是填充整個視圖的背景圖像一樣。在NIB中,不應將UIImageView設置爲伸展,這將爲您拉伸圖像並扭曲圖像,因爲只有在寬度保持不變時高度發生變化才能使圖像變形。你需要做的是用相同的增量調整高度和寬度,然後將圖像左移相同的增量以再次居中。這使得它在兩側都有一點變大,同時使其擴展到給定框架的整個高度。

我這樣調用它...

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

我這樣做是viewDidLoad中通常如果圖像在NIB設置。但是我也有在運行時下載並以這種方式顯示的圖像。這些圖像使用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; 
} 
+0

感謝回覆, – Mukesh