Curnelious
Curnelious

Reputation: 1

UIView changing the size of the image?

I am trying to add UIImage to UIView . The image is exactly in the size of the view -as i defined the view rect . Somehow i see that the images is displayed wider,and taller(its stretched ) . Why does my view is change the image size ?

UIImage *strip=[UIImage imageNamed:@"albumStrip.png"];
    UIImageView *imageView=[[UIImageView alloc]initWithImage:strip];
     UIView * aView =  [ [UIView alloc] initWithFrame:CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10)  ];
    aView.backgroundColor = [UIColor whiteColor];
    aView.tag  = 31000;
    aView.layer.cornerRadius=1;
    [aView addSubview:imageView];

EDIT : I can see that my image is 640x960. is it possible that for the iPhone4 the UIImage dont know how to take it and div it by factor 2 ?

Upvotes: 2

Views: 233

Answers (3)

Rifinio
Rifinio

Reputation: 943

of course your image is going to be stretched and can not be shown in your view. Because its frame is a lot bigger than the size of the UIView. you should set the frame of the UIImageView to be the same size as your UIView, the add it as a subView:

UIImage *strip=[UIImage imageNamed:@"albumStrip.png"];
CGRect frame = CGRectMake(0.03*winSize.width, 0.85*winSize.height , 0.95*winSize.width, winSize.width/10);
// create the uiimageView with the same frame size as its parentView.
UIImageView *imageView=[[UIImageView alloc] initWithFrame:frame];
// set the image
imageView.image = strip;
// create the view with the same frame
UIView * aView =  [ [UIView alloc] initWithFrame:frame];
aView.backgroundColor = [UIColor whiteColor];
aView.tag  = 31000;
aView.layer.cornerRadius=1;
[aView addSubview:imageView];

and of course if you can make the size of the uiimageview smaller ;)

Upvotes: 1

mmackh
mmackh

Reputation: 3630

Try setting the UIImageView's ContentMode (http://developer.apple.com/library/ios/#documentation/uikit/reference/UIImageView_Class/Reference/Reference.html)

imageView.contentMode = UIViewContentModeScaleAspectFit;

Upvotes: 3

Rahul Patel
Rahul Patel

Reputation: 5896

use

imageView.layer.masksToBounds = YES; which will restrict image to be wide and taller

If the masksToBounds property is set to YES, any sublayers of the layer that extend outside its boundaries will be clipped to those boundaries. Think of the layer, in that case, as a window onto its sublayers; anything outside the edges of the window will not be visible. When masksToBounds is NO, no clipping occurs, and any sublayers that extend outside the layer's boundaries will be visible in their entirety (as long as they don't go outside the edges of any superlayer that does have masking enabled).

Upvotes: 2

Related Questions