2016-03-02 22 views
0

如何獲得只針對其圖像從url.Please幫助下載獲得此如何獲得圖片僅供其圖像從URL下載數據

if(image) 
{ 
    CGFloat oX = (([[UIScreen mainScreen]bounds].size.width) -200)/3; 
    UIImageView *iView = [[UIImageView alloc]initWithFrame:CGRectMake(oX, yValue, 200, height+80)]; 
    [Utility getAndSaveImagesForUrl:str]; 

    [iView setImage:image]; 
    [self.scrollInfo addSubview:iView]; 
    [iView release]; 
    yValue = yValue + height+90; 


} 
else 
{ 
    [Utility getAndSaveImagesForUrl:str]; 
    UILabel * locationLabel = [[UILabel alloc]initWithFrame:CGRectMake(3, yValue, width, height+7)]; 
    [locationLabel setFont:[UIFont boldSystemFontOfSize:15.0f]]; 
    locationLabel.font=[UIFont systemFontOfSize:20.0f];//Kashif 
    [locationLabel setBackgroundColor:[UIColor clearColor]]; 
    locationLabel.text = info.location; 
    locationStr = info.location; 
    [locationLabel setTextColor:[UIColor colorWithRed:66.0/255.0f green:182.0/255.0f blue:241.0/255.0f alpha:1.0]]; 
    [self.scrollInfo addSubview:locationLabel]; 
    [locationLabel release]; 
    yValue = yValue + height+7; 
} 

回答

1

按照該數據圖像從邏輯上講,您需要將圖像存儲爲在Document目錄中創建的獨立文件夾。像下面:

-(NSString*)ImageFolderInDocDirectoryForURL 
{ 

    NSError *error; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
    NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/URL"]; 

    if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]) 
     [[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error]; //Create folder 

    return dataPath; 
} 

而且你可以在此文件夾中通過下面的代碼保存圖片:

NSString *Path = [[self ImageFolderInDocDirectory] stringByAppendingPathComponent:[NSString stringWithFormat:@"Myimage.png"] 
[[NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]] writeToFile:Path atomically:YES]; 

所以,當你想從URL下載thouse圖像只得到像下面的代碼的所有圖片:

-(void)geturlImage{ 

    _items = [NSMutableArray new]; 
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder 
    NSString* FoldePath=[documentsDirectory stringByAppendingPathComponent:@"URL"];//FolderPath 
    NSError * error; 
    NSArray * PhotoOFselectedAlbum = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:FoldePath error:&error]; 

     for(NSString* str in PhotoOFselectedAlbum) 
     { 
      [_items addObject:[FoldePath stringByAppendingPathComponent:str]]; 
     } 

    //NSLog(@"image is %@",_items); 


}