Set UIImageView size?

I have the following problem: I create subviews of UIView in 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 from an array. Now I want to resize the image to a specific size, but that will not work. any advice?

thanks + hi

+3
source share
3 answers

UIImagemissing method setSize: NSImage, but this post seems to add a method scaleToSize:using categories on UIImage.

+4
source

Change tmpView.frame to use the new CGRect:

tmpView.frame = CGRectMake(tmpView.frame.origin.x,
                           tmpView.frame.origin.y,
                           newWidth,
                           newHeight);
+4
source

:

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

CGRectMake...

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

X, Y. , :

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

, .

:

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

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

Source: https://habr.com/ru/post/1742338/


All Articles