0

I have a problem resizing images from web service (in code below i'm trying to get 400*266 down to 300*200). In detail view controller's viewDidLoad I add scroll view and image view. Then I add image to image view and change the size of image view's frame. Result is 300*100 image. Height is always about half of what I want. But if I change dimensions manually in storyboard (not useful for different image dimensions) it works like a charm. What am I missing?

@interface DetailVIewController () <UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;
....    

//adding image and resizing imageview's frame
UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:someURL]]];
imageView.image = image;
self.imageView.frame = CGRectMake(10, 20, 300, 200);
....

//further down the code (if it's an issue):
CGSize size = {screenWidth, screenHeight};
self.scrollView.delegate = self;
self.scrollView.contentSize = size;
frankC
  • 5
  • 6

2 Answers2

0

if you have images with different dimensions, then you need to look at in concept of scale rather than dimension. You adjust the scale of the image as you initialize it, have a look here. Then you when you set the image to the imageView call [imageView sizeToFit] to make it as fit exactly as the image size.

hope this helps.

Community
  • 1
  • 1
KDaker
  • 5,899
  • 5
  • 31
  • 44
  • weird...this didn't help either. actualy,the bigger number i enter for scale, closer to square my picture gets :/ i tried this: UIImage *originalImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:someURL]]]; // scaling set to 2.0 makes the image 1/2 the size. UIImage *scaledImage = [UIImage imageWithCGImage:[originalImage CGImage] scale:2.0 orientation:UIImageOrientationUp]; even if i enter dimensions in code,image doesn't seem to follow my instructions.any other ideas? – frankC Sep 04 '12 at 19:12
  • well scaling to 2.0 makes hte image double the size. You need to scale to 0.5. – KDaker Sep 04 '12 at 19:17
  • maybe any other suggestion on scaling image size down to little less than screen width (300px)? – frankC Sep 04 '12 at 19:18
  • actually it makes it half the size.tried it.bigger the number,smaller the image. – frankC Sep 04 '12 at 19:18
  • well... thats wierd. you can play around with the scale factor to get the size you want.. are you still calling `self.imageView.frame = CGRectMake(10, 20, 300, 200)` ? you need to get rid of that if you are using `sizeToFit`. – KDaker Sep 04 '12 at 19:24
  • i commented it out now when i have size to fit. tried this: [link](http://stackoverflow.com/questions/1703100/resize-uiimage-with-aspect-ratio/1703210#1703210) with no luck.if i scale down enough i get a square :/ – frankC Sep 04 '12 at 19:28
  • found the problem.tab bar and navigation bar.when i copied my code (tried also all possible solutions) to fresh project it all works great (including suggested solutions).when i embed in navigation controller image loses in height, if i add tabs it loses some more height (without touching the code which manages size).is this line `CGRect screenRect = [[UIScreen mainScreen] bounds]` the problem when using navigation or tab bar? – frankC Sep 04 '12 at 19:49
0

I finnaly solved the problem. I clicked on MainStoryboard.storyboard and I fiddled with it a bit. When I unchecked Resize View From NIB ... vuola ... image was perfect size :D

(click on view controller in storyboard -> Attributes Inspector -> View Controller -> Layout -> Resize View From NIB)

frankC
  • 5
  • 6