0
votes

Hi Im having a problem with adding an image to a button.Images are stored in a NSMutableArray property.The problem is to add and display the image to the button. I have verified that the image is added into the array. After I take an image from camera then I select to use it App will crash with the following error:

* Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIImage length]: unrecognized selector sent to instance 0x2090b5a

Code:

displayArray=[[NSMutableArray alloc]initWithObjects:@"MedsTime_MedPic.png",@"",@"",@"", nil];

-(void)addImage1:(NSNotification*)notify
{
    butImage=xapp.medicineImage;
    [img setImage:xapp.medicineImage forState:UIControlStateNormal];
    [displayArray replaceObjectAtIndex:0 withObject:xapp.medicineImage ];
    [table reloadData];
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];

    }
    else
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
    }

    if (indexPath.section==0)
    {
        img=[UIButton buttonWithType:UIButtonTypeRoundedRect];
        img.frame=CGRectMake(25, 15, 75, 75);
        img.layer.cornerRadius=6.0;
        img.clipsToBounds=YES;
        img.layer.borderColor=[[UIColor grayColor]CGColor];
        [cell.contentView addSubview:img];
        if (xapp.isEdited==YES)
        {

        }
        else if (xapp.isEdited==NO)
        {
            [img setImage:[UIImage imageNamed:[displayArray objectAtIndex:0]] forState:UIControlStateNormal];

        }

        [cell.contentView addSubview:img];

        img.titleLabel.textColor=[UIColor blackColor];
        text=[[UITextField alloc]initWithFrame:CGRectMake(105, 30, 200, 30)];
        text.delegate=self;
        text.placeholder=@"Enter Medicine First";

        if (xapp.isEdited==YES)
        {
            text.text=[[editArray objectAtIndex:0]valueForKey:@"medicineName"];
        }
        else{

            text.text=[displayArray objectAtIndex:1];
        }


        [text setBorderStyle:UITextBorderStyleRoundedRect];
        [cell addSubview:text];
        [cell addSubview:img];


    }
}
3
Why are you adding the 'img' object to both the cell and its contentView ? Also, why are you adding the img to the contentView twice ? - Petar
add what does it crash exactly? - Pfitz
Can you also post the code in didSelectRowForIndexPath ? ( I guess this is where the crash happens) - Petar

3 Answers

0
votes

Replace your code.Add one new array then check.Hope its useful for you.

if (xapp.isEdited==YES)
         {
             CamImage*eventImage = (CamImage*)[editArray objectAtIndex:0];
             [img setImage:[eventImage valueForKey:@"medicineImage"] forState:UIControlStateNormal];
             }

       else if (xapp.isEdited==NO)
           NSLog(@"editedmo");
         {
             if(Medimage==NO){
                 UIImage *i=(UIImage*)[displayArray objectAtIndex:0];
                 [img setImage:i forState:UIControlStateNormal];
            [img setImage:[UIImage imageNamed:[displayArray objectAtIndex:0]] forState:UIControlStateNormal];
                 }
0
votes

You should know you can't add image to UIButtonTypeRoundedRect, you should change it to custom button.

And in this function, you replace the first item of display array with an image, whereas other items in display array are nsstrings which seems to be the root of problems:

-(void)addImage1:(NSNotification*)notify
{
    butImage=xapp.medicineImage;
    [img setImage:xapp.medicineImage forState:UIControlStateNormal];
    [displayArray replaceObjectAtIndex:0 withObject:xapp.medicineImage ];
    [table reloadData];
}

Apart from some parts I don't understand in your code. There are some obvious mistakes:

This part conflicts with the uitableview conventions, you can remove it without getting errors.

else
{
    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue2 reuseIdentifier:CellIdentifier];
}

You add 'img' to contentView twice, once is enough.

0
votes

You (or some method in the sdk) are asking the image to perform the method length

reason: '-[UIImage length]: unrecognized selector sent to instance 0x2090b5a

An UIImage does not have the method length. According to the documentation this is a method for NSString and/or UIAElementArray.

My guess is you are using the image where a NSString or UIAElementArray is expected.