在fo​​r循环中动态创建的UIImageView的 [英] dynamic creation of uiimageview in for loop

查看:106
本文介绍了在fo​​r循环中动态创建的UIImageView的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  我是新来的iPhone。我已经保存。我想知道如何显示的UIImageView,其中的UIImageView应当按文件列表计数增加动态创建,在循环这些图像在阵列中的图像。这是code

 的NSArray *文件清单;
*的NSFileManager filemgr;
诠释计数;
INT I = 0;
INT T = 0;
filemgr =的NSFileManager defaultManager]
文件清单= [filemgr directoryContentsAtPath:@/ mypath中/];
NSMutableArray里* imageVwArr = [[NSMutableArray里的alloc]初始化];
数= [文件列表计数]
算上++;
对于(i = 0; I<计数;我++)
{
    *的UIImageView = imgVw [[UIImageView的页头] initWithImage:[文件列表objectAtIndex:我]];
    [imgVw setUserInteractionEnabled:YES];
    [imgVw setTag:我]。
    imgVw.frame = CGRectMake(T,0,480,320);
    [imageVwArr ADDOBJECT:imgVw];
    [self.view addSubview:imgVw];
    [imgVw发布]
    T = + 480;
}

它显示的错误:
扔NSException的一个实例后终止叫
程序接收到的信号:SIGABRT

在这里我做错了,我有没有关于创建动态的UIImageView想法
 在此先感谢....


解决方案

  [filemgr directoryContentsAtPath:@/ mypath中/];

这很可能会回到零。因为这个路径不存在。你不必访问完整的文件系统上的iphone。

把文件放入应用程序文件目录来代替。

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


编辑:我只是尝试这样做,并没有引发异常,所以你的错误是其他地方结果。
EDIT2:你增加计数,为什么呢?通过增加算你的for循环试图访问一个对象,它是不是在数组中了。

如果您有fileArray 10个条目数将是10。然后你增加数量,因此,计算将是11,但你的阵列仍只有10项。结果
您的循环将在索引0开始(这是你的第一个文件名),直到它访问的索引9(这是你的第10名),一切正常。

在下一循环它试图在指数10来访问的文件名和这个索引出界。

EDIT3:

下一页错误: initWithImage:想要一个UIImage的,而不是一个文件名。用途:

 的NSString * FULLPATH = [yourImagePath stringByAppendingPathComponent:[文件列表objectAtIndex:我]];
* UIImage的图像= [[[UIImage的ALLOC] initWithContentsOfFile:FULLPATH]自动释放]
*的UIImageView = imgVw [[UIImageView的页头] initWithImage:图片];

EDIT4:
也许你想创建imageViews只有当UIImage的可以从文件创建。所以,如果碰巧有一个文件,该文件是不是有效的图像不创建一个空的ImageView的:

 如果(!图片)
    继续;
*的UIImageView = imgVw [[UIImageView的页头] initWithImage:图片];


&EDIT5放大器; LAST:
我的一个实施 imageViewController 的。请不要使用它,因为它是,试着去了解它,并学到一些东西。它是远离完美。

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

解决方案

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

这篇关于在fo​​r循环中动态创建的UIImageView的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆