2016-12-16 29 views
-2

我有網址的數組:終止應用程序由於未捕獲的異常「NSInvalidArgumentException」,原因是:「 - [__ NSCFString isFileURL]:無法識別的選擇發送到實例

imageurl = 

    (
     "http://10.1.1.4:8084/Photos/AA/c6aee8617ec94116911e17f745ced4d8.jpg", 
     "http://10.1.1.4:8084/Photos/AA/75764b74fbc440c790ff235e5336223e.jpg", 
     "http://10.1.1.4:8084/Photos/AA/4b390e733d8c48a6931079120af60b0a.jpg", 
     "http://10.1.1.4:8084/Photos/AA/2fade8440ae74b4dabdaff5dc13c5128.jpg" 
    ); 

我試圖實現與水平滾動視圖使用以下代碼在網址上的圖像:

- (void)viewDidLoad { 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view. 
    int pageCount=4; 
    _scroller.pagingEnabled=YES; 
    _scroller.contentSize=CGSizeMake(pageCount*_scroller.bounds.size.width,_scroller.bounds.size.height); 
    CGRect ViewSize=_scroller.bounds; 

    NSArray *imgArray = [self.tripDetails valueForKey:@"Flightimageurl"]; 


    for(int i=0;i<[imgArray count];i++) 
    { 

     UIImageView *imgView1=[[UIImageView alloc]initWithFrame:ViewSize]; 

     NSURL *url=[imgArray objectAtIndex:i]; 
     NSData *data = [NSData dataWithContentsOfURL:url]; 


     imgView1.image=[UIImage imageWithData:data]; 
     [_scroller addSubview:imgView1]; 
     [self.view addSubview:_scroller]; 
     ViewSize =CGRectOffset(ViewSize,_scroller.bounds.size.width,0); 

    } 

但是,這崩潰並給出了上述例外。如何解決這個問題,並且可以在imageView上顯示來自url的圖像。

+0

'[imgArray objectAtIndex:i]'返回'NSString',而不是'NSUrl'。 – shallowThought

回答

3

您收到此崩潰,因爲你imgArray包含NSString對象不NSURL對象,所以首先你需要從NSString實例創建NSURL實例。

NSURL *url = [NSURL URLWithString:[imgArray objectAtIndex:i]]; 
相關問題