How to get images for only that data for which images are downloaded from the url

时间:2016-03-02 10:47:30

标签: ios

How to get images for only that data for which images are downloaded from the url.Please help for getting this

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 个答案:

答案 0 :(得分: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下载的图像时,只需获取所有图像,如下面的代码:

-(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);


}