作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将 UIImage
对象添加到数组中,但它们没有被添加。请帮忙
我的代码:
imageArray = [[NSMutableArray alloc] init];
arry = [[NSMutableArray alloc] init];
[arry addObject:@"http://adjingo.2cimple.com/content/151/Image/6291.jpg"];
[arry addObject:@"http://adjingo.2cimple.com/content/151/Image/6290.jpg"];
//Fetch images from web and store in another array
for (int i=0; i<[arry count]; i++)
{
NSString *string=[arry objectAtIndex:i];
NSURL *url=[NSURL URLWithString:string];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
if ( !error )
{
UIImage *image = [[UIImage alloc] initWithData:data];
//Store image in imageArray
[imageArray insertObject:image atIndex:i];
} else{
}
}];
}
NSLog(@"%lu",(unsigned long)[imageArray count]); //this line is always showing 0 as count
当我打印包含 UIImage
对象的数组的 count
时,它显示 0。
(我实际上想一次下载数组中的图像,然后将其作为幻灯片显示在 UIImageView
中)。
请指导我。谢谢!
最佳答案
有几个问题。
[[NSMutableArray alloc] init]
不分配内存,不能添加超出当前大小的项目。 [NSMutableArray arrayWithCapacity:size]
确实分配了内存,对象可以插入到任何索引处,直到并包括当前大小。请参阅 NSMutableArray
文档。
在 sendAsynchronousRequest
block 之后没有图像被下载,这将在下载完成时发生。
同时开始大量下载可能会出现性能问题。
关于ios - 如何在 iOS 中将 UIImage 对象添加到 NSMutableArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30828474/
我是一名优秀的程序员,十分优秀!