我正在从webService解析JSON,它给我图像,文本等。文本可以被提取出来,但是如何从中获取图像呢?
这就是JSON响应的样子。
[
{ "photo": "23_841676772.jpg",
"date": "2013-06-06 08:11:15",
"tags": "",
"ID_article": "1",
"commentcount": "5"
},
]
我试图在tableView:cellForRow方法中将其设置为无效。
NSMutableDictionary *tempDict3 = [self.jsonArray objectAtIndex:indexPath.row];
cell.blogImageView.image = [UIImage imageNamed:[tempDict3 objectForKey:@"photo"]];
答案 0 :(得分:1)
要从JASON回复中取消照片名称,您可以使用此代码
JSONResponse : [
{ "photo": "23_841676772.jpg",
"date": "2013-06-06 08:11:15",
"tags": "",
"ID_article": "1",
"commentcount": "5"
},
]
在.h
文件
@interface ViewController : UIViewController
{
NSMutableArray * photoNameData;
}
在.m
档案中
photoNameData =[[NSMutableArray alloc] init];
NSMutableArray * tmpAry = JSONResponse;
if (tmpAry.count !=0) {
for (int i=0; i<tmpAry.count; i++) {
NSMutableDictionary * tmpDictn = [tmpAry objectAtIndex:i];
[photoNameData addObject:[tmpDictn objectForKey:@"photo"]];
}
}
您可以使用以下代码下载文档目录中的图像。
for (int i=0; i<photoNameData.count; i++) { //updated here
//First check that document directory contain image or not
NSString* tmpStr = [photoNameData objectAtIndex:i]; // used the photoNameData to get image name
NSString* tmpImgURL = [NSString stringWithFormat:@"%@%@",@"http://www.my server.com/web_services/photos/",tmpStr]; //merge the image name and url
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documents = [paths objectAtIndex:0];
NSString* foofile = [documents stringByAppendingPathComponent:tmpStr];
BOOL imageExists = [[NSFileManager defaultManager] fileExistsAtPath:foofile];
// if image not exist than download it
if (!imageExists) {
NSLog(@"%@ not exist",tmpImgURL);
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:tmpImgURL] cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10.0];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *err){
if (!err && data) {
//NSLog(@"data : %@",data);
NSString *finalPath = [documents stringByAppendingPathComponent:tmpStr];
NSLog(@"finalPath : %@",finalPath);
[data writeToFile:finalPath atomically:YES];
}else{
NSLog(@"err : %@",err);
}
}];
}
}
您可以使用此代码使用此图片..
NSString* tmpStr = [photoNameData objectAtIndex:indexPath.row]; //get the image name from photoNameData
NSString * documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
UIImage * image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@",documentsDirectoryPath, tmpStr]];
cell.blogImageView.image = image;
答案 1 :(得分:1)
我使用静态json你可以用你的json url替换str。
NSString *Str =[NSString stringWithFormat:@"[{\"photo\": \"23_841676772.jpg\",\"date\":\"2013-06-06 08:11:15\",\"tags\": \"\",\"ID_article\": \"1\",\"commentcount\": \"5\"},{\"photo\": \"dfdgfdgd.jpg\",\"date\":\"2013-06-06 08:11:15\",\"tags\": \"\",\"ID_article\": \"1\",\"commentcount\": \"5\"}]"];
NSDictionary *catgDict = [NSJSONSerialization JSONObjectWithData:[Str dataUsingEncoding:NSUTF8StringEncoding] options:kNilOptions error:nil];
for(NSDictionary *dict in catgDict)
{
NSLog(@"dd: %@",dict);
NSLog(@"Photo: %@",[dict valueForKey:@"photo"]);
}
答案 2 :(得分:1)
请您的网络服务团队在您的回复中添加另一个标记“image_url”,以便您可以将图片网址转换为图片。如果他们说我们出于安全原因无法给出它,那么剩下的唯一选择就是对路径进行硬编码并将“图像名称”附加到它上面,这样你就可以获得完整的图像网址,现在可以将其转换为图像。