开发者

dynamic creation of uiimageview in for loop

开发者 https://www.devze.com 2023-01-19 12:02 出处:网络
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 in

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 addSubvie开发者_JAVA百科w: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....


[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.

0

精彩评论

暂无评论...
验证码 换一张
取 消