使用iCarousel与文件夹中的图像

时间:2014-07-02 12:42:18

标签: ios objective-c icarousel

我有一个文件夹(Documents / Images)用于存储从网上下载的图像,当启动iCarsousel的视图时,还会发送使用哪些图像的信息,因此只会使用该文件夹中的某些图像。 但是由于某些原因,下面的代码似乎不起作用,并且显示了一个空白视图,并且没有给出错误消息。

- (NSUInteger)numberOfItemsInCarousel:(iCarousel *)carousel
{
    [carousel setType:iCarouselTypeCylinder];


    [self getImages];
    return [images count];
}

- (void)getImages{
    images=[[NSMutableArray alloc]init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:databaseName];
    db = [FMDatabase databaseWithPath:writableDBPath];
    if (![db open]) {
        return;
    }
    NSLog(@"getting images");
    NSLog(_galleryid);
    FMResultSet *result = [db executeQuery:@"SELECT * FROM mediaImages WHERE galleryID = ?;", _galleryid];
    while ([result next]){

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

        //configure carousel
        NSString *fPath = [documentsDirectory stringByAppendingPathComponent:@"Images"];

        NSString *filepath = [fPath stringByAppendingString:@"/"];
        filepath = [filepath stringByAppendingString:filename];
        NSLog(filepath);
        [images addObject:filepath];
    }
}

- (UIView *)carousel:(iCarousel *)carousel viewForItemAtIndex:(NSUInteger)index reusingView:(UIView *)view
{
    view = [[UIView alloc] init];
    view.contentMode = UIViewContentModeScaleAspectFit;
    CGRect rec = view.frame;
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
    {
        rec.size.width = 250;
        rec.size.height = 250;
    }
    view.frame = rec;
    UIImageView *iv;
    if(UI_USER_INTERFACE_IDIOM()==UIUserInterfaceIdiomPhone)
    {
        iv=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    }
    NSString *temp=[images objectAtIndex:index];
    iv.image=[UIImage imageNamed:temp];
    iv.contentMode = UIViewContentModeScaleAspectFit;
    [view addSubview:iv];
    return view;
}

- (void)carousel:(iCarousel *)carousel didSelectItemAtIndex:(NSInteger)index
{
    NSLog(@"Image is selected.");
}

- (CGFloat)carousel:(iCarousel *)carousel valueForOption:(iCarouselOption)option withDefault:(CGFloat)value
{
    switch (option)
    {
        case iCarouselOptionWrap:
        {
            return YES;
        }
        default:
        {
            return value;
        }
    }
}

代码是否存在明显错误,或者是调试问题的好方法?

1 个答案:

答案 0 :(得分:0)

我的猜测是问题在于:

iv.image=[UIImage imageNamed:temp];

temp是文件的路径,而不是图片的名称,所以这不会起作用。也许您想要使用+ imageWithContentsOfFile:代替?

如果这不是唯一的问题,我强烈建议您尝试使用日志记录或调试来进一步隔离它;例如,如果您记录了temp的值以及从imageNamed:返回的图像,那么您可能也会看到此问题。