本文介绍了如何在主目录中保存图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am making an application in which i have use Json parsing. With the help of json parsing i get photo url which is saved in string. To show images in my cell i use this code

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];

现在问题是,当我回去或转发时,我等待几秒钟回来相同的观点。现在我想要的是当我首先使用应用程序然后我等待该屏幕,否则从主目录获取图像。我如何将这些图像保存在我的主目录中?如何从主目录访问?

Now prblem is that when i go back or forword then i have wait for few second to come back on same view. Now i want that i when application is used first tme then i wait for that screen otherwise get images from home directory. How i save these image in my home directory? How access from home directory?

推荐答案

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

You can use this to write a file to your Documents Folder

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

}

这篇关于如何在主目录中保存图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-13 22:31