2014-04-06 5 views
0

받은 JSon에서 작동하도록 UIImage를 가져 오는 데 문제가 있습니다. 여기의 내 JSON 여기JSON에서 UIImage 가져 오기

[ 
{ 
node_title: "Fumi", 
node_uid: "11", 
users_node_name: "pae", 
nid: "7", 
Body: "<p>This is Fumi Restaurant</p> ", 
Enterprise Address: "99/9", 
Enterprise Email: "[email protected]", 
Enterprise Tel: "08111111", 
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fumi.jpg" width="300" height="300" alt="" />" 
}, 
{ 
node_title: "Fuji", 
node_uid: "8", 
users_node_name: "testent", 
nid: "1", 
Body: "<p>Fuji</p> ", 
Enterprise Address: "Somwhere it belong", 
Enterprise Email: "[email protected]", 
Enterprise Tel: "02-999-9999", 
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />" 
} 
] 

내 코드를 살펴 보자 : 내가받은 모든 JSON을 얻을이의 ViewController에 그들을 보여줄 수 있어요. 그러나 [self.detail valueForKey : @ "Enterprise Logo"]는 JSON의 내용으로 인해 작동하지 않는 것 같습니다.

BTW, self.detail은 준비 작업을 수행하여 이전 UITableViewController에서 전달한 Json 객체입니다.

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 

    self.EnterPriseName.text = [self.detail valueForKey:@"node_title"]; 
    self.EnterPriseTel.text = [self.detail valueForKey:@"users_node_name"]; 
    self.EnterPriseBody.text = [self.detail valueForKey:@"Body"]; 
    self.EnterPriseEmail.text = [self.detail valueForKey:@"nid"]; 

    [self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]]; 

} 

잘 작동하는이 방법을 사용해보세요.하지만 Enterprise 로고의 콘텐츠에는 해당 img의 URL 만 포함되어야합니다. 가능한 한 동적 인 일을하려고하므로 고정 응답을 기대하지 않습니다. 이 문제를 해결할 수있는 방법이 있습니까?

[self.picLogo setImageWithURL:[NSURL URLWithString:@"http://localhost/drupal/sites/default/files/Fumi.jpg"]]; 
+2

이는 합법적 인 JSON이 아닙니다. 그것은 구문 분석하지 않습니다. –

+0

@HotLicks 어떻게 합법적으로 만들 수 있습니까? – user3027109

+1

다른 쪽에서 합법적 인 JSON을 생성하도록하십시오. –

답변

0

을 가지고 사용하여 이미지를 요청하면 알려주세요 :

URL을 추출하려면이 링크로 찾아보세요 NSURLConnection, NSURLSession 또는 AFNetworking.

NSString * __block imageURL = nil; 
NSArray * objects = [[self.details valueForKey:@"Enterprise Logo"] componentsSeparatedByString:@" "]; 

[objects enumerateObjectsUsingBlock:^(id object, NSUInteger index, BOOL *stop) 
{ 
    NSRange range = [object rangeOfString:@"src="]; 
    if (range.location != NSNotFound) 
    { 
     imageURL = [object substringFromIndex:range.location+range.length]; 
     *stop = YES; 
    } 
}]; 
+0

당신의 방법을 따르지 만 작동하지 않습니다. NSLog로 imageURL을 출력했는데 여전히 null입니다. – user3027109

+1

@HotLicks가 그의 코멘트에서 언급했듯이 JSON은 합법적이지 않으므로 구문 분석을하지 않아서 null 값을 얻게됩니다. – Jonathan

+1

다음 @HotLicks 덧글 에 따라 작업을 진행했습니다. 도움 주셔서 대단히 감사합니다! 정말 고마워. – user3027109

2
Enterprise Logo: "<img typeof="foaf:Image" src="http://localhost/drupal/sites/default/files/Fuji.png" width="320" height="320" alt="" />" 

기업 로고 키의 베일은 JSON에서 HTML입니다.

[self.picLogo setImageWithURL:[NSURL URLWithString:[self.detail valueForKey:@"Enterprise Logo"]]]; 

사용중인 코드가 해당 이미지에 대한 URL을 찾고 있습니다. html이 아닙니다.

최상의 방법은 json에게 이미지 URL이있는 것입니다.

http://localhost/drupal/sites/default/files/Fuji.png 

및 그 안에 html이 아닙니다.

+0

설명해도 어떻게 해석합니까? –

0

이미지의 URL이 될 img 태그 인 Enterprise Logo의 src 값을 추출하십시오. retrive image url from string

방금 ​​이미지 URL을 추출 Enterprise Logo의 값을 검색 한 후 어떤 문제 :

관련 문제