我有UIScrollView
设置,可以从网络上加载图片。滚动视图中的UIImageView
宽度为320像素,但不知何故,图像比此宽度更小或更大,并且在其他图像视图中可见时会发生干涉。
滚动视图中的每个图片如何精确地为320像素并且不会干扰其他图片
我使用以下代码:
#pragma mark - ScrollView Pics
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
UICollectionReusableView *reusableview = nil;
if (kind == UICollectionElementKindSectionHeader) {
UIView *headerView =[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
reusableview = (UICollectionReusableView*)headerView;
[self addSlideShow:reusableview at:indexPath];
}
return reusableview;
}
-(void)addSlideShow:(id)reusableView at:(NSIndexPath *)indexPath {
int spacing = 4;
int headerSpacing = (indexPath.section == 0) ? spacing : 8; // 8 = spacing + spacing
int topSpacing = headerSpacing - spacing;
float headHeight = self.customLayout.headerReferenceSize.height - headerSpacing;
float pControlHeight = 20;
self.myScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, topSpacing, self.view.frame.size.width, headHeight)];
self.myScrollView.showsHorizontalScrollIndicator = NO;
self.myScrollView.delegate = self;
[reusableView addSubview:self.myScrollView];
self.myPageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, headHeight - pControlHeight, self.view.frame.size.width, pControlHeight)];
self.myPageControl.numberOfPages = 4;
self.myPageControl.enabled = TRUE;
self.myPageControl.highlighted = TRUE;
[reusableView addSubview:self.myPageControl];
[self myScrollingAt:(int)indexPath.section];
pageControlBeingUsed = NO;
}
-(void)myScrollingAt:(int)section {
self.myScrollView.pagingEnabled = YES;
[self.myScrollView setIndicatorStyle:UIScrollViewIndicatorStyleWhite];
int numberOfViews = 4;
for (int i=0; i < numberOfViews; i++) {
CGFloat xOrigin = i * self.myScrollView.frame.size.width;
UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(xOrigin, 0, self.myScrollView.frame.size.width, self.myScrollView.frame.size.height)];
imageView.contentMode = UIViewContentModeScaleAspectFill;
imageView.userInteractionEnabled = YES;
int itemOfScrollView = [self getItem:i atSection:section];
if (self.sshare.coData.count > 0 && itemOfScrollView < self.sshare.coData.count) {
[self setPhotos:imageView onItem:itemOfScrollView];
}
imageView.tag = 4000 + itemOfScrollView; // create tag to get the item to navigate to the detailView
[self addTap:imageView];
DLog(@"imageview %d: %@", i, imageView);
[self.myScrollView addSubview:imageView];
}
self.myScrollView.contentSize = CGSizeMake(numberOfViews * self.myScrollView.frame.size.width, self.myScrollView.frame.size.height);
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
pageControlBeingUsed = NO;
}
- (int)getItem:(int)item atSection:(int)section {
return item + (section * 4); // return the positon where I have to look in my array
}
- (void)addTap:(id)view {
UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapToDetailView:)];
paidTap.numberOfTapsRequired = 1;
[view addGestureRecognizer:myTap];
}
-(void)setPhotos:(UIImageView *)imageView onItem:(int)item{
NSString *imageUrl = [self.sshare myImage:item];
[imageView setImageWithURL:[NSURL URLWithString:[self.sshare myImage:item]]];
}
[742:60b] -[ViewController myScrollingAt:] [Line 293] imageview 0: <UIImageView: 0x155502e0; frame = (0 0; 320 222.976); tag = 4000; gestureRecognizers = <NSArray: 0x15550680>; layer = <CALayer: 0x15550360>>
[742:60b] -[ViewController myScrollingAt:] [Line 293] imageview 1: <UIImageView: 0x1565fe30; frame = (320 0; 320 222.976); tag = 4001; gestureRecognizers = <NSArray: 0x1565f650>; layer = <CALayer: 0x15660260>>
[742:60b] -[ViewController myScrollingAt:] [Line 293] imageview 2: <UIImageView: 0x155508a0; frame = (640 0; 320 222.976); tag = 4002; gestureRecognizers = <NSArray: 0x155509a0>; layer = <CALayer: 0x155506b0>>
[742:60b] -[ViewController myScrollingAt:] [Line 293] imageview 3: <UIImageView: 0x155509d0; frame = (960 0; 320 222.976); tag = 4003; gestureRecognizers = <NSArray: 0x15550c00>; layer = <CALayer: 0x15550a50>>
答案 0 :(得分:2)
由于您正在使用UIViewContentModeScaleAspectFill
,因此您想剪切图片:
imageView.clipsToBounds = YES;
Aspect-fill缩放图像(保持其宽高比)以在绘制时完全填充可见区域。如果图像视图未设置为剪辑,则可能会在其边界之外绘制。