views:

53

answers:

1

Hi i am new to iphone.. I have stored the images in array .I want to know how to display those images in UIImageView ,where the UIImageView should be created dynamically according to filelist count increases, in for loop. this is the code

NSArray *filelist;
NSFileManager *filemgr;
int count;
int i=0;
int t=0;
filemgr = [NSFileManager defaultManager];
filelist = [filemgr directoryContentsAtPath: @"/Mypath/"];
NSMutableArray *imageVwArr = [[NSMutableArray alloc]init];  
count = [filelist count];
count++;
for(i = 0; i < count; i++)                                
{
    UIImageView *imgVw = [[UIImageView alloc] initWithImage:[filelist objectAtIndex:i]];
    [imgVw setUserInteractionEnabled:YES];
    [imgVw setTag:i];
    imgVw.frame=CGRectMake(t, 0, 480, 320);
    [imageVwArr addObject:imgVw];
    [self.view addSubview:imgVw];
    [imgVw release];
    t=t+480;        
}

it shows the error: terminate called after throwing an instance of 'NSException' Program received signal: “SIGABRT”.

where i did wrong i have no idea about creating dynamic uiimageview thanks in advance....

+1  A: 
[filemgr directoryContentsAtPath: @"/Mypath/"];

Most likely this will return nil. Because this path does not exist. You don't have access to the complete file system on the iphone.

Put your files into the applications Document directory instead.

NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];

EDIT: I just tried this, and no Exception is raised, so your bug is somewhere else.
EDIT2: You are incrementing count, why? By incrementing count your for-loop tries to access an object which is not in the array anymore.

If your fileArray has 10 entries count will be 10. Then you increment count, so count will be 11, but your array still only has 10 entries.
Your for loop will start at index 0 (which is your first filename) until it accesses index 9 (which is your 10th filename) everything works.

In the next loop it tries to access the filename at index 10. And this index is out of bounds.

EDIT3:

Next bug: initWithImage: wants an UIImage, not a filename. Use:

NSString *fullPath = [yourImagePath stringByAppendingPathComponent:[filelist objectAtIndex:i]];
UIImage *image = [[[UIImage alloc] initWithContentsOfFile:fullPath] autorelease]
UIImageView *imgVw = [[UIImageView alloc] initWithImage:image];

EDIT4: Maybe you want to create your imageViews only if an UIImage could be created from your file. So if it happens that there is a file which is not a valid image you don't create an empty imageview.:

if (!image)
    continue;
UIImageView *imgVw = [[UIImageView alloc] initWithImage:image];

EDIT5&LAST: my implementation of an imageViewController. Please don't use it as it is, try to understand it and learn something. It is far away from perfect.

fluchtpunkt
k thanks a lot using for loop how i create multiple imageviews when i scroll the imageview i want to view all the images how i get ... thanks in advance
muthiah
there is a file " .DS_Store" they wont appear but the arraylist contain this file so only i increase the count
muthiah
your forloop does exact this, it creates multiple imageviews. add them to an uiview and display this uiview in an uiscrollview.
fluchtpunkt
NEVER mess with count if you want to access objects in an array. Use the approach in my 4th edit. Check if the image could be created. Since no image could be created from .DS_Store, simply don't display it.
fluchtpunkt
based on the count i want to create uiimageview dynamically ...
muthiah
k fine i will use the edit4
muthiah
thanks a lot if u have any idea abt this please share i confused this module in 2 days.
muthiah
fluchtpunkt
thanks for your help.
muthiah
where i store the files how i get the files by using this NSString *documentDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
muthiah
Of course you have to save them in the exact same directory. How to get them there depends how you get the images, if you fetch them from the internet just save them there, if you provide them within your app bundle, you have to copy them, or change the imagepath to the app bundles image directory. Btw, would be nice if you would accept my answer ;-)
fluchtpunkt
i apply the sample code that u give but " NSString *fullPath = [path stringByAppendingPathComponent:fullPath];"the path contains nill ..when the for loop run one time it contain that images in the hit of second time loop the UIIMAGE contain nil(0*0)...
muthiah