Reputation: 477
I have method that download and resize image according to screen size to fit image fullscreen. Max image size is (320x416). Now, I have fullscreen UIScrollView, in which I am creating UIImageView with initWithFrame shifted on X by screen width, adding UIScrollView space horizontally. No problem at all as long as I set UIImageView created inside UIScrollView [imgView setContentMode:UIViewContentModeLeft]. With [imgView setContentMode:UIViewContentModeCenter] it shifts image, every count of loop, add half screen to right, so I can see just first image, half of second and third only if I went behind scrollView bounds a bit.
Here is my code, can you someone kick me where am I missing something? Thank you. I put NSLog under imgView initWithFrame:CGRectMake but coords are OK, X position is shifting by 320 pixels and arrange imgView in scrollView is all right then.
- (void)setImages {
int screenWidth = self.view.frame.size.width;
int screenHeight = self.view.frame.size.height;
// I know count of images here => imageIndex
[mainView setContentSize:CGSizeMake(screenWidth * imageIndex, screenHeight)];
int actualWidth = screenWidth;
for (int index = 0; index < imageIndex; index++) {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(actualWidth - screenWidth, 0, actualWidth, screenHeight)];
ImageProcessing *impc = [[ImageProcessing alloc] init];
UIImage *image = [UIImage imageWithData:[imagesNSDataArray objectAtIndex:index]];
UIImage *resampled = [impc processImageToWidthAndHeight:image :screenWidth :screenHeight];
// [imgView setContentMode:UIViewContentModeCenter];
[imgView setContentMode:UIViewContentModeLeft];
imgView.image = resampled;
[mainView addSubview:imgView];
actualWidth = actualWidth + screenWidth;
}
}
Upvotes: 0
Views: 1036
Reputation: 130092
You are mixing view width and horizontal position. The views don't have the same width - you are making them wider and wider. When you try to center the image inside the view, it gets shifted because the view is wider than you think.
int viewX = 0;
for (int index = 0; index < imageCount; index++) {
CGRect frame = CGRectMake(viewX, 0, screenWidth, screenHeight);
UIImageView *imgView = [[UIImageView alloc] initWithFrame:frame];
[...]
viewX += screenWidth;
}
Upvotes: 2