【问题标题】:Picture does not show in iCarousel [duplicate]图片未显示在 iCarousel [重复]
【发布时间】:2013-08-30 09:51:25
【问题描述】:

我正在使用 iCarousel 来显示我的图像,但奇怪的是我正确地编写了代码,但没有出现任何内容。我正在尝试显示目录​​文件夹中的图像。在目录文件夹中,我有几个文件夹中保存了图像。有什么遗漏吗?我快到了,我做不到。另外,我在 UIView 的最后一个括号中有一个警告,说控件到达非无效函数的末尾。这与这个问题有关吗?我已经退货了,但它不起作用。这是我下面的代码。

-(void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSFileManager *fileManager  = [NSFileManager defaultManager];

    //configure carousel1
    NSString *fPath = [documentsDirectory stringByAppendingPathComponent:@"Tops"];
    NSArray *directoryContent = [fileManager directoryContentsAtPath: fPath];
    imageArray1 = [directoryContent mutableCopy];

    //configure carousel2
    fPath = [documentsDirectory stringByAppendingPathComponent:@"Bottoms"];
    directoryContent = [fileManager directoryContentsAtPath:fPath];
    imageArray2 = [directoryContent mutableCopy];

    carousel1.type = iCarouselTypeLinear;
    carousel2.type = iCarouselTypeLinear;
}

- (void)viewDidUnload
{
    [super viewDidUnload];

    //free up memory by releasing subviews
    self.carousel1 = nil;
    self.carousel2 = nil;
}

#pragma mark -
#pragma mark iCarousel methods

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    //return the total number of items in the carousel
    if (carousel == carousel1)
    {
        return [imageArray1 count];
    }
    else
    {
        return [imageArray2 count];
    }
}
- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{

    //create new view if no view is available for recycling
    if (view == nil)
    {
        view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];

        UIImage *image;
        if (carousel == carousel1)
        {
            image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
            ((UIImageView *)view).image = image;
        }
        else
        {
            image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
            ((UIImageView *)view).image = image;
        }
            return view;
    }}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    //customize carousel display
    switch (option)
    {
        case iCarouselOptionSpacing:
        {
            if (carousel == carousel2)
            {
                //add a bit of spacing between the item views
                return value * 1.05f;
            }
        }
        default:
        {
            return value;
        }
    }
}

如果您想查看更多代码,请随时询问。

#import <UIKit/UIKit.h>
#import "iCarousel.h"


@interface iCarouselExampleViewController : UIViewController <iCarouselDataSource, iCarouselDelegate>{
    NSMutableArray *allImagesArray;
    NSMutableArray *imageArray1;
    NSMutableArray *imageArray2;
}

@property (nonatomic, retain) IBOutlet iCarousel *carousel1;
@property (nonatomic, retain) IBOutlet iCarousel *carousel2;

@end

【问题讨论】:

  • 您看到任何视图/图像吗?您是否检查过调用的方法以及图像数组中是否包含图像?
  • 我看不到视图和图像,所以它真的很奇怪。我只能看到背景。我已经放入了 2 个视图并在我的标题中声明了它。是的,数组中应该有图像。我还将更新我的问题,并放置头文件。

标签: ios directory icarousel


【解决方案1】:

编辑 - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view 方法中的代码,如下所述--

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
if (view == nil)
{
    view = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 200.0f, 200.0f)];

    UIImage *image;
    if (carousel == carousel1)
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
    else
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
        return view;
}
else {
    UIImage *image;
    if (carousel == carousel1)
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray1 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
    else
    {
        image  = [UIImage imageWithContentsOfFile:[imageArray2 objectAtIndex:index]];
        ((UIImageView *)view).image = image;
    }
}


return view;
}

问题可能出在您的代码中,如果 (view != nil) 则您的代码没有实现在 icarousel 中为视图设置图像。

【讨论】:

  • 这行不通,因为从它得到错误说预期方法体预期':'预期标识符或(预期标识符或(无关的右大括号(“}”))
  • 是的,它会抛出错误,因为您需要在此代码之前添加下面提到的行 - - (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view {
  • 现在我已经编辑了答案,以便您可以复制和粘贴它。它会起作用的。但不要忘记接受答案并进行标记。
猜你喜欢
  • 2013-09-03
  • 1970-01-01
  • 2021-10-22
  • 2023-03-28
  • 1970-01-01
  • 2017-07-02
  • 1970-01-01
  • 2017-09-28
  • 2017-12-21
相关资源
最近更新 更多