Ios 未将NSURLConnection响应添加到可变数组
下面的代码在我的头文件中有什么问题?我有一个属性:Ios 未将NSURLConnection响应添加到可变数组,ios,objective-c,nsurlconnection,Ios,Objective C,Nsurlconnection,下面的代码在我的头文件中有什么问题?我有一个属性: @property(非原子,保留)NSMutableArray*imgArray 当我查看末尾的NSLog时,“imgArray”总是空的。我试图在UIImageView子类中提取多个图像以制作动画“gif”。问题是数组总是空的,因此它不会添加任何图像,因为数组是空的 在我的.m文件中: @implementation AnimatedOverlay BOOL isValid; @synthesize imgArray; - (id)in
@property(非原子,保留)NSMutableArray*imgArray代码>
当我查看末尾的NSLog
时,“imgArray”总是空的。我试图在UIImageView
子类中提取多个图像以制作动画“gif”。问题是数组总是空的,因此它不会添加任何图像,因为数组是空的
在我的.m
文件中:
@implementation AnimatedOverlay
BOOL isValid;
@synthesize imgArray;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
[self startAnimatingWithFrame:frame];
}
return self;
}
-(void) startAnimatingWithFrame:(CGRect)frame
{
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
NSDate *today = [NSDate date];
NSDate *hourAgo = [[NSDate alloc] initWithTimeInterval:-3600 sinceDate:today];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSString *dateString = [dateFormat stringFromDate:today];
[dateFormat setDateFormat:@"HH"];
NSString *hourString = [dateFormat stringFromDate:today];
[dateFormat setDateFormat:@"HH"];
NSString *hourString2 = [dateFormat stringFromDate:hourAgo];
imgArray = [[NSMutableArray alloc] init];
for(int i=0;i<=5;i++){
NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://radar.weather.gov/ridge/Conus/RadarImg/southeast_%@_%@%i8_N0Ronly.gif",dateString,hourString,i]];
NSURLRequest* request = [NSURLRequest requestWithURL:fileUrl];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,NSData * data,NSError * error) {
if ([data length] >0 && error == nil){
UIImage *image = [UIImage imageWithData:data];
if(image!=nil){
[imgArray addObject:image];
}
}else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
}
NSLog(@"%@",imgArray);
self.animationImages = [[NSArray alloc] initWithArray:imgArray];
self.animationDuration = 1.0f;
self.animationRepeatCount = 0;
[self startAnimating];
}
@implementation AnimatedOverlay
BOOL是有效的;
@合成imgaray;
-(id)initWithFrame:(CGRect)帧
{
self=[super initWithFrame:frame];
如果(自我){
//初始化代码
[带帧的自启动图像:帧];
}
回归自我;
}
-(无效)开始使用帧进行图像处理:(CGRect)帧
{
NSDateFormatter*dateFormat=[[NSDateFormatter alloc]init];
NSDate*今天=[NSDate日期];
NSDate*hourAgo=[[NSDate alloc]initWithTimeInterval:-3600 sinceDate:today];
[dateFormat setDateFormat:@“yyyyymmdd”];
NSString*dateString=[dateFormat stringFromDate:today];
[日期格式setDateFormat:@“HH”];
NSString*hourString=[dateFormat stringFromDate:today];
[日期格式setDateFormat:@“HH”];
NSString*hourString2=[dateFormat stringFromDate:hourAgo];
imgArray=[[NSMutableArray alloc]init];
对于(int i=0;i0&&error==nil){
UIImage*image=[UIImage imageWithData:data];
如果(图像!=nil){
[imgaray addObject:image];
}
}else if([数据长度]==0&&error==nil){
NSLog(@“未下载任何内容”);
}else if(错误!=nil){
NSLog(@“Error=%@”,Error);
}
}];
}
NSLog(@“%@”,imgArray);
self.animationImages=[[NSArray alloc]initWithArray:imgArray];
self.animationDuration=1.0f;
self.animationRepeatCount=0;
[自启动];
}
for(int i=0;i0&&error==nil){
UIImage*image=[UIImage imageWithData:data];
如果(图像!=nil){
[imgaray addObject:image];
}
}else if([数据长度]==0&&error==nil){
NSLog(@“未下载任何内容”);
}else if(错误!=nil){
NSLog(@“Error=%@”,Error);
}
//在完成处理程序块内使用imgArray,现在您正在块外使用它
NSLog(@“%@”,imgArray);
self.animationImages=[[NSArray alloc]initWithArray:imgArray];
self.animationDuration=1.0f;
self.animationRepeatCount=0;
[自启动];
}];
}
希望这对您有用。正在添加。但是它是异步的,所以在记录阵列很久之后就会添加它。那么我如何解决这个问题呢?我是否使用非异步?除了日志记录之外,您还有什么问题?我添加了完整的代码,并用一个更好的问题编辑了上面的文章。下载一个图像,然后您执行动画。或者下载所有图像,然后执行动画?
for(int i=0;i<=5;i++){
NSURL *fileUrl = [NSURL URLWithString:[NSString stringWithFormat:@"http://radar.weather.gov/ridge/Conus/RadarImg/southeast_%@_%@%i8_N0Ronly.gif",dateString,hourString,i]];
NSURLRequest* request = [NSURLRequest requestWithURL:fileUrl];
[NSURLConnection sendAsynchronousRequest:request
queue:[NSOperationQueue mainQueue]
completionHandler:^(NSURLResponse * response,NSData * data,NSError * error) {
if ([data length] >0 && error == nil){
UIImage *image = [UIImage imageWithData:data];
if(image!=nil){
[imgArray addObject:image];
}
}else if ([data length] == 0 && error == nil){
NSLog(@"Nothing was downloaded.");
}else if (error != nil){
NSLog(@"Error = %@", error);
}
//Use your imgArray inside the completion handler block, right now you are using it outside the block
NSLog(@"%@",imgArray);
self.animationImages = [[NSArray alloc] initWithArray:imgArray];
self.animationDuration = 1.0f;
self.animationRepeatCount = 0;
[self startAnimating];
}];
}