2013-08-15 2 views
0

Я пытаюсь установить изображение для ImageView с помощью URLпочему изображение не отображается в ImageView

Я попытался ниже код ...

NSString *imgURL = [NSString stringWithFormat:@"http://sahhtna.hardtask.org/Files/Clients/thumb/2e10ec86-f323-4889-a996-00cf6758f354.JPG"]; 
NSLog(@"imageURL===%@", imgURL); 
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]; 
NSLog(@"=====%@", data); 
[myImageView setImage:[UIImage imageWithData:data]]; 

Это прекрасно работает.

Теперь я пробовал URL-адрес с данными из фида и пробовал ниже.

NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]]; 
NSLog(@"imageURL===%@", imgURL); 
NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:imgURL]]; 
NSLog(@"=====%@", data); 
[myImageView setImage:[UIImage imageWithData:data]]; 

Это дает мне вывод, как показано ниже.

2013-08-15 11:22:56.725 Sahhtna[7555:11303] imgURL==http://sahhtna.hardtask.org/Files/Clients/thumb/2e10ec86-f323-4889-a996-00cf6758f354.JPG 
2013-08-15 11:22:56.726 Sahhtna[7555:11303] =====(null) 

Я не понимаю, почему я получаю данные как (null), когда строки одинаковы? Поскольку данные null, я не получаю никакого изображения в своем UIImageView.

Любая идея, что происходит не так?


Когда я NSLog(@"[NSURL URLWithString:imgURL]==%@", [NSURL URLWithString:imgURL]); я получаю выход как [NSURL URLWithString:imgURL]==(null)


я получил причину. В конце строки есть Enter (новая строка). Как его удалить?

У меня есть решение.

Были введенные и пробелы.

NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]]; 
imgURL = [imgURL stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
imgURL = [imgURL stringByReplacingOccurrencesOfString:@" " withString:@""]; 
+0

вы NSLog силам ли для [NSURL URLWithString: imgURL]? – Leta0n

+0

@ Leta0n: его значение null –

+0

Попробуйте прямо сравнивать строки imgURL, чтобы увидеть, может ли быть разница. – ThomasW

ответ

1

Были введенные и пробелы. Ниже был трюк.

NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]]; 
imgURL = [imgURL stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
imgURL = [imgURL stringByReplacingOccurrencesOfString:@" " withString:@""]; 
0

Попробуйте это:

replaceOccurrencesOfString:@"\\n" withString:@"" 

replaceOccurrencesOfString:@" " withString:@"" 
+0

yep .. вот что я прокомментировал ... –

+0

Да ........ man .. –

1

Попробуйте это,

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    NSString *imgURL = [NSString stringWithFormat:@"%@", [[feeds objectAtIndex:indexPath.row] objectForKey:@"Image"]]; 
    [self downloadingServerImageFromUrl:myImageView AndUrl:imgURL]; 
} 

-(void)downloadingServerImageFromUrl:(UIImageView*)imgView AndUrl:(NSString*)strUrl 
{ 

NSString* theFileName = [NSString stringWithFormat:@"%@.jpg",[[strUrl lastPathComponent] stringByDeletingPathExtension]]; 
NSFileManager *fileManager =[NSFileManager defaultManager]; 
NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]]; 


imgView.backgroundColor = [UIColor clearColor]; 
UIActivityIndicatorView *actView = [[UIActivityIndicatorView alloc]initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhite]; 
[imgView addSubview:actView]; 
[actView startAnimating]; 
CGSize boundsSize = imgView.bounds.size; 
CGRect frameToCenter = actView.frame; 
// center horizontally 
if (frameToCenter.size.width < boundsSize.width) 
    frameToCenter.origin.x = (boundsSize.width - frameToCenter.size.width)/2; 
else 
    frameToCenter.origin.x = 0; 

// center vertically 
if (frameToCenter.size.height < boundsSize.height) 
    frameToCenter.origin.y = (boundsSize.height - frameToCenter.size.height)/2; 
else 
    frameToCenter.origin.y = 0; 

actView.frame = frameToCenter; 


dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
dispatch_async(queue, ^{ 

    NSData *dataFromFile = nil; 
    NSData *dataFromUrl = nil; 

    dataFromFile = [fileManager contentsAtPath:fileName]; 
    if(dataFromFile==nil){ 
     NSString *url =[strUrl stringByReplacingOccurrencesOfString:@"\n" withString:@""]; 
     url=[url stringByReplacingOccurrencesOfString:@"\t" withString:@""]; 
     url=[url stringByReplacingOccurrencesOfString:@" " withString:@""]; 

     url = [url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
     NSError* error = nil; 
     dataFromUrl = [NSData dataWithContentsOfURL:[NSURL URLWithString:url] options:NSDataReadingUncached error:&error]; 

     if (error) { 
      NSLog(@"%@", [error localizedDescription]); 
     } else { 
     } 
    } 

    dispatch_sync(dispatch_get_main_queue(), ^{ 

     if(dataFromFile!=nil){ 

      myImageView.image=[UIImage imageWithData:dataFromFile]; 
     }else if(dataFromUrl!=nil){ 
      myImageView.image=[UIImage imageWithData:dataFromUrl]; 
      NSString *fileName = [NSHomeDirectory() stringByAppendingPathComponent:[NSString stringWithFormat:@"tmp/%@",theFileName]]; 

      BOOL filecreationSuccess = [fileManager createFileAtPath:fileName contents:dataFromUrl attributes:nil]; 
      if(filecreationSuccess == NO){ 
       NSLog(@"Failed to create the html file"); 
      } 

     }else{ 
      myImageView.image=[UIImage imageNamed:@"no_image.jpg"]; 
     } 
     [actView removeFromSuperview]; 
    }); 
}); 
} 
+0

спасибо за ваш ответ ... –

Смежные вопросы