dynamic creation of uiimageview in for loop

有些话、适合烂在心里 提交于 2019-12-06 14:51:42

问题


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:


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



来源:https://stackoverflow.com/questions/3914579/dynamic-creation-of-uiimageview-in-for-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!