views:

665

answers:

3

hi,

i have following problem: i generate subviews UIView in an UIScrollView including UIImageViews.

NSArray *bilder = [[NSArray alloc] initWithObjects:@"lol.jpg", @"lol2.jpg", nil];
NSArray *added = [[NSMutableArray alloc] init];
UIImageView *tmpView;

for (NSString *name in bilder) {
    UIImage *image = [UIImage imageNamed:name];
    tmpView = [[UIImageView alloc] initWithImage:image];
    tmpView.contentMode = UIViewContentModeScaleAspectFit;
    tmpView.frame = CGRectMake(0, 0, 300, 300);

    [added addObject:tmpView];

    [tmpView release];
}

CGSize framy = mainFrame.frame.size;

NSUInteger x = 0;

for (UIView *view in added) {
    [mainFrame addSubview:view];
    view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);
}

mainFrame.scrollEnabled = YES;
mainFrame.pagingEnabled = YES;
mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);
mainFrame.backgroundColor = [UIColor blackColor];

i get image file names out of an array. now i want to resize the imageview to a specific size, but it won't work. any advice?

thanks + regards

A: 

Change tmpView.frame to use a new CGRect:

tmpView.frame = CGRectMake(tmpView.frame.origin.x,
                           tmpView.frame.origin.y,
                           newWidth,
                           newHeight);
MrHen
well, that' actually the same code i used before, isn't it? doesn't work either :(
Tronic
Try using CGRectOffset to simply move the frame down. That should keep the original size intact. Something that may also help is temporarily setting the background colors of all the views to make sure that everything other than the UIImageView is displaying where you want it to be.
MrHen
A: 

What I see is:

view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);

But if you look at CGRectMake...

Declaration: CGRect CGRectMake ( CGFloat x, CGFloat y, CGFloat width, CGFloat height );

You are setting the X value, not the Y value. I expect, based on your later call to:

mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);

that you intend to make the these images appear on top of each other, not side by side.

Suggest:

view.frame = CGRectMake(framy.height * x++, 0, framy.height, framy.width);

to

view.frame = CGRectMake(0,framy.height * x++, framy.height, framy.width);

Kenny
it's landscape mode, they appear side by side, but fullscreen. but i don't want it fullscreen, i want 300x300!
Tronic
Then it leads me to:mainFrame.contentSize = CGSizeMake(framy.height * [added count], framy.width);Would you not want:mainFrame.contentSize = CGSizeMake(framy.height, framy.widtht * [added count];
Kenny
A: 

UIImage lacks the setSize: method of NSImage, but this post seems to add a scaleToSize: method using a category on UIImage.

Isaac
i tried this, image resizing works, but the imagebiréw stays fullscreen. how can i resize the imageview to the size of the resized image?
Tronic
Are you resizing the image before you call `[[UIImageView alloc] initWithImage:image]`? The way I read the documentation for `initWithImage:`, it should cause the `UIImageView` to match the image size.
Isaac
yeah, actually i make UIImage *image = [UIImage imageNamed:name]; then UIImage *smallImage = [image scaleToSize:CGSizeMake(100,100)]; and then i alloc the UIIMageView with tmpView = [[UIImageView alloc] initWithImage:smallImage];but the imageview keeps fullscreen, checked that with a backgroundcolor set!
Tronic
There are three things that I could see being possible causes (though they don't seem likely): 1. You're making the image 100x100, but the image view 300x300; 2. `tmpView.contentMode = UIViewContentModeScaleAspectFit;` (which shouldn't be needed with the `UIImage scaleToSize:`, I think); and 3. the possibility that `mainFrame` is somehow responsible for the mis-sizing when the `UIImageView` gets added as a subframe. Since I don't do any UIKit/iPhone stuff myself (just AppKit/MacOSX), I'm pretty much out of ideas beyond that.
Isaac
Oh, actually, 1 more idea to try to isolate the issue, if nothing else: log the frame of the `UIImageView` immediately after `initWithImage:`, immediately after `tmpView.frame = ...`, before and after `[mainFrame addSubview:view];`, and after `view.frame = ...` and see if that gives any clues as to where the view is ending up full-screen.
Isaac