0

我遇到了一个奇怪的问题,我似乎无法弄清楚。
在将多个子视图添加到 ScrollView 后,ScrollViews 会继续在常规图像上添加空白

顶部的空白

更多颜色会更好一些: 在此处输入图像描述

它应该是这样的: 没有空白

图像是完全可滚动的。

这是我用来创建滚动视图的代码:

-(void)addImagesToScrollView
{
  if (self.product != nil)
  {
    int imageCount = 0;

    if (self.product.labelImage != nil)
    {
      UIImageView *labelImage = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
      labelImage.image = [UIImage imageWithData:self.product.labelImage];
      labelImage.contentMode = UIViewContentModeScaleAspectFit;
      labelImage.tag = 100;
      [self.scrollView addSubview:labelImage];
      imageCount += 1;
    }

    if (self.product.bottleImage != nil)
    {
      UIImageView *bottleImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
      bottleImage.image = [UIImage imageWithData:self.product.bottleImage];
      bottleImage.contentMode = UIViewContentModeScaleAspectFit;
      bottleImage.tag = 101;
      imageCount += 1;

      [self.scrollView addSubview:bottleImage];
    }
    if (self.product.pourImage != nil)
    {
      UIImageView *pourImage = [[UIImageView alloc] initWithFrame:CGRectMake(self.scrollView.frame.size.width * 2, 0, self.scrollView.frame.size.width, self.scrollView.frame.size.height)];
      pourImage.image = [UIImage imageWithData:self.product.pourImage];
      pourImage.contentMode = UIViewContentModeScaleAspectFit;
      pourImage.tag = 102;
      imageCount += 1;
      [self.scrollView addSubview:pourImage];
    }
    self.scrollView.contentSize = CGSizeMake(self.scrollView.frame.size.width * imageCount, self.scrollView.frame.size.height);
    [self.scrollView setContentOffset:CGPointZero animated:NO];

  }
}

编辑:

使用背景颜色:

带背景色

4

2 回答 2

0

使用下面的代码,您将获得所需的输出。在以下代码中,您的应用将使用相册照片并根据相册中的图像数量自动设置滚动大小。并请根据您的图像存储库进行修改。

-(void)getAllPictures
{


    imageArray=[[NSArray alloc] init];
    mutableArray =[[NSMutableArray alloc]init];
    NSMutableArray* assetURLDictionaries = [[NSMutableArray alloc] init];

    library = [[ALAssetsLibrary alloc] init];

    void (^assetEnumerator)( ALAsset *, NSUInteger, BOOL *) = ^(ALAsset *result, NSUInteger index, BOOL *stop) {
        if(result != nil) {
            if([[result valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto]) {
                [assetURLDictionaries addObject:[result valueForProperty:ALAssetPropertyURLs]];

                NSURL *url= (NSURL*) [[result defaultRepresentation]url]; 

                [library assetForURL:url
                         resultBlock:^(ALAsset *asset) {
                             [mutableArray addObject:[UIImage imageWithCGImage:[[asset defaultRepresentation] fullScreenImage]]];

                             if ([mutableArray count]==count)
                             {
                                 imageArray=[[NSArray alloc] initWithArray:mutableArray];
                                 [self allPhotosCollected:imageArray];
                             }
                         }
                        failureBlock:^(NSError *error){ NSLog(@"operation was not successfull!"); } ]; 

            } 
        }
    };

    NSMutableArray *assetGroups = [[NSMutableArray alloc] init];

    void (^ assetGroupEnumerator) ( ALAssetsGroup *, BOOL *)= ^(ALAssetsGroup *group, BOOL *stop) {
        if(group != nil) {
            [group enumerateAssetsUsingBlock:assetEnumerator];
            [assetGroups addObject:group];
            count=[group numberOfAssets];
        }
    };

    assetGroups = [[NSMutableArray alloc] init];

    [library enumerateGroupsWithTypes:ALAssetsGroupAll
                           usingBlock:assetGroupEnumerator
                         failureBlock:^(NSError *error) {NSLog(@"There is an error");}];
}



-(void)allPhotosCollected:(NSArray*)imgArray
{


    CGSize imageSize;

    imageSize.width= imageView.bounds.size.width;
    imageSize.height = imageView.bounds.size.height;
    self.scrollView=[[UIScrollView alloc]init];
    self.scrollView.frame = imageView.frame;
    self.scrollView.contentSize = CGSizeMake(imageSize.width * imgArray.count, imageSize.height);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces=YES;
    self.scrollView.scrollEnabled = YES;
    self.scrollView.autoresizingMask = UIViewAutoresizingFlexibleBottomMargin;
    [self.scrollView flashScrollIndicators];
    [self.view addSubview:self.scrollView];

    CGFloat xPos = 0.0;

    for (UIImage *image in imgArray) {
            imageView = [[UIImageView alloc] initWithImage:image];
            imageView.frame = CGRectMake(xPos, 0.0, imageSize.width, imageSize.height);


         [self.scrollView addSubview:imageView];

        xPos += imageSize.width;
        // assuming ARC, otherwise release imageView

    }



}


#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 20,320 , 420)];
    [self getAllPictures];
}
于 2013-10-11T06:32:33.827 回答
0

利用:

bottleImage.layer.color = [UIcolor blueColor].CGColor; 
bottleImage.layer.borderWidth = 2.0f; 

它会让你看到你的 UIImageView 的框架,也许你计算不正确的框架

于 2013-10-10T23:06:01.550 回答