我想在网格视图中显示我的图像。我使用了UICollection视图控制器,我正在以JSON格式传递数据。我无法使用JSON格式显示图像。任何人都可以找到我的错误吗?这是我的代码
@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
我的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 :(得分:0)
创建一个tableViewCell类,并将该类名分配给实用程序区域中collectionView的单元格。在tableViewCell类中创建imageView的出口,并在cellForItemAtIndexPath委托方法中访问它。 然后使用以下代码分配图像:
[cell. recipeImageView sd_setImageWithURL:[NSURL URLWithString:[[array objectAtIndex:indexPath.row] valueForKey:@"image"]]];
这肯定会帮到你。
答案 1 :(得分:0)
不要使用<?php
$result = exec("perl test.pl &");
echo $result;
header("Location: newpage.php") ;
?>
转义您的网址,而是在您的JSON中使用百分比编码,并使用以下代码将其解码回未转义的字符串
\
另一个要点:由于您要使用
NSString *imageUrl = @"http%3A%2F%2Fwesotech.com%2Fweb%2Fmyrecipe%2Frecipeimages%2F2016080614704658059030.jpg"; NSString *url = [imageUrl stringByRemovingPercentEncoding];
加载图片,因此您应该在http://
下的Allow Arbitrary Loads
设置为YES
{1}}文件。另外,请注意,不建议设置允许任意加载,因为这会导致安全问题。如果你这样会更好 可以让您的所有网络电话都使用
App Transport Security Settings