我正在制作一个使用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; }更多推荐
如何在主目录中保存图像?
发布评论