2016-08-23 156 views
1

我想在網格視圖中顯示我的圖像。我已經使用UICollection視圖控制器,這是我傳遞的JSON格式的數據,我不能夠使用JSON格式顯示圖像。任何人都可以找到我的錯誤?這裏是我的代碼將Json值傳遞給UICollection查看

@interface ReceipeCollectionViewController() 
{ 
    NSMutableData *webdata; 
    NSURLConnection *connection; 
    NSMutableArray *contentarray; 
    NSMutableArray *array; 
} 

@end 

@implementation ReceipeCollectionViewController 

(void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [array removeAllObjects]; 
    [[self collectionView]setDelegate:self]; 
    [[self collectionView]setDataSource:self]; 
    array = [[NSMutableArray alloc] init];  
} 

(void)viewDidAppear:(BOOL)animated 
{ 
    NSString *[email protected]"http://wesotech.com/web/myrecipe/web_services/recipe_listing.php?"; 
    NSURL *url=[NSURL URLWithString:serverurl]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    connection = [NSURLConnection connectionWithRequest:request delegate:self]; 
    if(connection) 
    { 
     webdata=[[NSMutableData alloc]init]; 
    } 
    else 
    { 
     NSLog(@"Connection failure"); 
    } 
} 

(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [webdata appendData:data]; 
} 

(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    NSLog(@"Fail with error"); 
} 

(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    NSError *error; 
    NSArray *results=[NSJSONSerialization JSONObjectWithData:webdata options:0 error:&error]; 
    [array removeAllObjects]; 
    [array addObjectsFromArray:results]; 
    [[self collectionView]reloadData]; 
} 

(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [webdata setLength:0]; 
} 

(void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
} 

(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView 
{ 
    return 1; 
} 

(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section 
{ 
    return [array count]; 
} 

(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath 
{ 
    [self.mySpinner stopAnimating]; 

    static NSString *identifier = @"Cell"; 

    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath]; 

    UIImageView *recipeImageView = (UIImageView *)[cell.contentView viewWithTag:100]; 

    recipeImageView.image=[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] valueForKey:@"image"]]]]; 

    UITextField *recipeTitleView =(UITextField *)[cell.contentView viewWithTag:101]; 

    recipeTitleView.text=[[array objectAtIndex:indexPath.row]valueForKey:@"recname"]; 

    return cell; 
} 

@end 

我的JSON格式是,

[ 

    {"image":"http:\/\/wesotech.com\/web\/myrecipe\/recipeimages\/2016080614704658059030.jpg","recname":"Italian Chicken","recid":"1"}, 
    {"image":"http:\/\/wesotech.com\/web\/myrecipe\/recipeimages\/2016080614704653421637.jpg","recname":"Fast Food Friday","recid":"2"}, 
    {"image":"http:\/\/wesotech.com\/web\/myrecipe\/recipeimages\/2016080614704648912893.jpg","recname":"Honey Clazed Lamb Chops","recid":"3"} 

] 
+0

您添加的痕跡或斷點,如果你得到了正確的URL來檢查,如果圖像正確加載中...?另外,你的代碼並不能完全顯示'UIImageView'來自何處/如何。你是否也檢查過它的限制?它可能有正確的圖像,但只有0尺寸或類似的東西。使用視圖調試器將提供更多信息。 – jcaron

回答

0

而是與\逸出您的網址,用百分比編碼在JSON和使用下面的代碼

NSString *imageUrl = @"http%3A%2F%2Fwesotech.com%2Fweb%2Fmyrecipe%2Frecipeimages%2F2016080614704658059030.jpg"; 
NSString *url = [imageUrl stringByRemovingPercentEncoding]; 

另外重要的一點是解碼迴轉義字符串:因爲你是加載圖像與http://,你應該在的info.plist文件中設置Allow Arbitrary LoadsYES

另外,請記住,不允許設置允許任意加載,因爲這會導致安全問題。這將是更好,如果你 可以把所有的網絡電話使用https://

+0

只要知道[蘋果將於2016年底前需要iOS應用的HTTPS連接](https://techcrunch.com/2016/06/14/apple-will-require-https-connections-for-ios-apps- by-the-end-of-2016 /) – Sudo

+0

非常感謝你的工作 –

0

使類tableViewCell和公用事業領域分配一個類名的CollectionView的細胞。在tableViewCell Class中創建imageView的出口,並在cellForItemAtIndexPath委託方法中訪問它。 然後指定圖像與此代碼:

[cell. recipeImageView sd_setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] valueForKey:@"image"]]]; 

這一定會幫助你。