开发者

UIImageView: full screen image

I want to see a full screen image on the iPhone. how many pixels I have to set it? width and height are different for iphone 3 and 4?

My source code:

NSString *filePath = [[NSBundle mainBundle] pathForResource:theProduct.image ofType:@"png"]; 
UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageWithContentsOfFile:filePath]];
img.frame = CGRectMake(0.0, 0.0, ?,开发者_如何学运维 ?);
[self.view addSubview:img];

What should I put instead of question marks?

Many thanks, Stefano


Try setting the UIImageView's frame to [[UIScreen mainScreen] applicationFrame].

That will set the UIImageView to use all of the space available to the app, excluding the status bar if its visible.


You should set it to the size of the older iPhones: 320 x 480 pixels. The iPhone 4 will then double this to fit the screen. If you don't want to see pixelation you will need to have another @2x image sized 640 x 960.


The question is already answered (use 320 x 480 or 320 x 460 after the status bar has been removed).

As a way to solve similar problems though: In Xcode drop a UIImageView in a xib; size it appropriately, then in the right pane Show the "Size inspector" (second tab in from the right). It gives the Frame Rectangle's width and height. Use this width and height as the size for an image if you don't want it scaled (or rather if you want a 1:1 scale). Also (as above) use double resolution images @2x for retina displays.


this is a pretty old thread so just updating how this is done with Constraints.

imageView?.translatesAutoresizingMaskIntoConstraints = false
imageView?.contentMode = .scaleAspectFit
view.backgroundColor=UIColor.blue
view.addSubview(self.imageView!)

    let height = NSLayoutConstraint(item: imageView!, attribute: .height, relatedBy: .equal, toItem: view, attribute: .height, multiplier: 1, constant: 0)
    let width = NSLayoutConstraint(item: imageView!, attribute: .width, relatedBy: .equal, toItem: view, attribute: .width, multiplier: 1, constant: 0)
    view.addConstraints([height, width])
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜