Sakthi Sakthi - 3 months ago 15
Objective-C Question

Pass Json Value to UICollection View

i want to display my images in grid view. i have used UICollection view controller,data which is i'm passing in JSON format.i cant able to display images using JSON format. Can anyone pls find my mistake? Here is my code

@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 *serverurl=@"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


My JSON Format is,

[

{"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"}

]

R P R P
Answer

Instead of escaping your URL with \, use percentage encoding in your JSON and decode it back to unescaped string using the below code

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

Another important point: Since you are loading an image with http://, you should set Allow Arbitrary Loads to YES under App Transport Security Settings in your info.plist file.

Also, make a note that it is not advisable to set allow arbitrary loads as it would result in security issues. It would be better if you could have all your network calls to use https://

Comments