【发布时间】:2015-08-29 23:07:05
【问题描述】:
我正在尝试将 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)。
请指导我。谢谢!
【问题讨论】:
-
您可以先将该图像转换为 NSData,然后将该数据添加到数组中。
-
@RajatDeepSingh 1. 注意接收到的图像是
NSData对象。 2.将NSImage对象保存在NSMutableArray中没有问题,只是数组中的指针。 -
@zaph ,请在下面查看我对您的回答的评论。
标签: ios objective-c uiimageview uiimage nsmutablearray