如何处理图像视图?

时间:2011-09-21 10:15:03

标签: iphone

我开发了一个应用程序,我在表格中使用了三个图像视图。对于前两个图像加载成功。在加载第三个图像时,图像将变黑。请告诉我如何处理这一个。下面是imageview声明的代码。

     - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
        UITableViewCell *cell=[[[UITableViewCell alloc]init] autorelease];
         tableview1.separatorStyle=UITableViewCellSeparatorStyleNone;
    if(indexPath.row==0)
    {
        image1=[[[UIImageView alloc]initWithFrame:CGRectMake(12, 8, 90, 90)]autorelease];
        image1.frame=CGRectMake(12, 8, 90, 90);
        [image1.layer setBorderWidth:1.0f];
        [image1.layer setBorderColor:[[UIColor grayColor] CGColor]];
        [image1.layer setCornerRadius:1.0f];
        image1.layer.masksToBounds = YES;
        image1.userInteractionEnabled=YES;
        image1.tag=1;

        [image1 setContentMode:UIViewContentModeScaleToFill];
        if([[odrdata stringForKey:@"keyTo1Image"] isEqualToString:@""])  
        {

        }
        else 
        {
    image1.image=[UIImage imageWithContentsOfFile:[odrdata stringForKey:@"keyTo1Image"]];

        }
        [cell.contentView addSubview:image1];

    image2=[[[UIImageView alloc]initWithFrame:CGRectMake(114, 8, 90, 90)]autorelease];
        image2.frame=CGRectMake(114, 8, 90, 90);
        [image2.layer setBorderWidth:1.0f];
        [image2.layer setBorderColor:[[UIColor grayColor] CGColor]];
        [image2.layer setCornerRadius:1.0f];
        image2.layer.masksToBounds = YES;
        image2.userInteractionEnabled=YES;
        image2.tag=1;

        [image2 setContentMode:UIViewContentModeScaleToFill];
        if([[odrdata stringForKey:@"keyTo2Image"] isEqualToString:@""])  
        {

        }
        else 
        {
    image2.image=[UIImage imageWithContentsOfFile:[odrdata stringForKey:@"keyTo2Image"]];

        }
        [cell.contentView addSubview:image2];

    image3=[[[UIImageView alloc]initWithFrame:CGRectMake(216, 8, 90, 90)]autorelease];
        image3.frame=CGRectMake(216, 8, 90, 90);
        [image3.layer setBorderWidth:1.0f];
        [image3.layer setBorderColor:[[UIColor grayColor] CGColor]];
        [image3.layer setCornerRadius:1.0f];
        image3.layer.masksToBounds = YES;
        image3.userInteractionEnabled=YES;
        image3.tag=3;

        [image3 setContentMode:UIViewContentModeScaleToFill];
        if([[odrdata stringForKey:@"keyTo3Image"] isEqualToString:@""])  
        {

        }
        else 
        {
    image3.image=[UIImage imageWithContentsOfFile:[odrdata stringForKey:@"keyTo3Image"]];
        }
        [cell.contentView addSubview:image3];

fsttapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlefstTap:)];
        fsttapGesture.delegate=self;
        [image1 addGestureRecognizer:fsttapGesture];

sndtapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handlesndTap:)];
        sndtapGesture.delegate=self;
        [image2 addGestureRecognizer:sndtapGesture];

trdtapGesture=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handletrdTap:)];
        trdtapGesture.delegate=self;
        [image3 addGestureRecognizer:trdtapGesture];

        return cell;
    }

}

1 个答案:

答案 0 :(得分:0)

您确定keyTo3Image的文件是否存在?可能发生的是图像视图无法在您在该字符串中指定的路径中找到图像。检查它是否实际指向文件。要验证这一点,请更改backgroundColor的{​​{1}}属性并查看其是否发生更改:

image3

如果它变为红色,则表示无法找到图像,因此只显示空图像视图。