ios - 如何仅获取从 url 下载图像的数据的图像

标签 ios

如何仅获取从 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;
}

最佳答案

按照逻辑,您需要将图像存储为在文档目录中创建的单独文件夹。像下面这样:

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


}

关于ios - 如何仅获取从 url 下载图像的数据的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35745001/

相关文章:

ios - 为 Facebook 移动应用安装广告安装 Facebook SDK

objective-c - UIImageview自定义闪屏框架问题

ios - 使用 iOS 在 PhoneGap 中打开文件(docx、pptx、pdf)

iOS - 使用 ScrollView 的奇怪约束

ios - 在 iPhone 上安装 Facebook 应用程序时,Facebook 登录失败

android - 他们是否根据屏幕大小设置图标/字体/等大小的任何规则

ios - UITableViewCell 问题的 Storyboard 布局

c# - Unity 3D "automatic"嘴唇同步?

iphone - iOS 中的闹钟应用程序

iPhone 应用程序 : In App Purchase