【问题标题】:How save images in home directory?如何将图像保存在主目录中?
【发布时间】:2011-11-13 19:33:51
【问题描述】:

我正在制作一个使用 Json 解析的应用程序。在 json 解析的帮助下,我得到了保存在字符串中的照片 url。要在我的单元格中显示图像,我使用此代码

NSString *strURL=[NSString stringWithFormat:@"%@", [list_photo objectAtIndex:indexPath.row]];
NSData *imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString: strURL]];
CGRect myImage =CGRectMake(13,5,50,50);
UIImageView *imageView = [[UIImageView alloc] initWithFrame:myImage];
[imageView setImage:[UIImage imageWithData: imageData]];
[cell addSubview:imageView];

现在的问题是,当我返回或前进时,我必须等待几秒钟才能返回相同的视图。现在我希望我在第一次使用应用程序时等待该屏幕,否则从主目录获取图像。我如何将这些图像保存在我的主目录中?如何从主目录访问?

【问题讨论】:

  • 看看this有没有帮助

标签: iphone image uiimageview nsarray nsdata


【解决方案1】:

您可以使用imageData将图像保存在默认文档目录中,如下所示;

// Accessing the documents directory
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"myImage.png"];

//Writing the image file
    [imageData writeToFile:savedImagePath atomically:NO];

【讨论】:

    【解决方案2】:

    您可以使用它来将文件写入您的文档文件夹

    +(BOOL) downloadFileFromURL:(NSString *) url withLocalName:(NSString*) localName
    {
    
        //Get the local file and it's size.
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:localName];
    
        NSError *error;
        NSDictionary *fileAttributes = [[NSFileManager defaultManager] attributesOfItemAtPath:finalPath error:&error];
        NSAssert (error == nil, ([NSString stringWithFormat:@"Error: %@", error]));
        if (error) return NO;
        int localFileSize = [[fileAttributes objectForKey:NSFileSize] intValue];
    
    
        //Prepare a request for the desired resource.
        NSMutableURLRequest *request = [NSMutableURLRequest
                                        requestWithURL:[NSURL URLWithString:url]];
        [request setHTTPMethod:@"HEAD"];
    
        //Send the request for just the HTTP header.
        NSURLResponse *response;
        [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
        NSAssert (error == nil, ([NSString stringWithFormat:@"Error: %@", error]));
        if (error) return NO;
    
        //Check the response code
        int status = 404;
        if ([response respondsToSelector:@selector(statusCode)])
        {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response;
            status = [httpResponse statusCode];
        }
        if (status != 200)
        {
            //file not found
            return NO;
        }
        else
        {
            //file found
        }
    
        //Get the expected file size of the downloaded file
        int remoteFileSize = [response expectedContentLength];
    
    
        //If the file isn't already downloaded, download it.
        if (localFileSize != remoteFileSize || (localFileSize == 0))
        {       
            NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
            [[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil];
            return YES;
        }
        //here we may wish to check the dates or the file contents to ensure they are the same file.
        //The file is already downloaded
        return YES;
    }
    

    然后阅读:

    +(UIImage*) fileAtLocation:(NSString*) docLocation
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString *finalPath = [documentsDirectory stringByAppendingPathComponent:docLocation];
    
    
        NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
        [[NSFileManager defaultManager] createFileAtPath:finalPath contents:data attributes:nil];
    
        NSData *databuffer = [[NSFileManager defaultManager] contentsAtPath:finalPath];     
        UIImage *image = [UIImage imageWithData:databuffer];
        return image;
    
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-04
      • 1970-01-01
      • 1970-01-01
      • 2012-08-13
      • 2021-06-02
      • 2015-07-04
      • 1970-01-01
      • 2015-12-26
      相关资源
      最近更新 更多