0

I have a base url and I have the image names in a array extracted from there server .I am using a collection view, I have to display all the 23 images in the collection view I have two problems

  1. when I try to access cell.imageview.image it says no property image view in cell but in custom cell.h file I have created the outlet and I am using storyboard

2.as one is string base url and other end path or filename of the images in array extracted from john from server

this is how I get from data--

    {
    "added_by" = 1;
    "category_id" = 182;
    "category_image" = "shots.png";
    "category_name" = Shots;
    sequance = 19;
    status = 0;
    "store_id" = 1;
},
    {
    "added_by" = 1;
    "category_id" = 168;
    "category_image" = "classiccocktail.png";
    "category_name" = "Classic Cocktails";
    sequance = 20;
    status = 0;
    "store_id" = 1;
},
    {
    "added_by" = 1;
    "category_id" = 167;
    "category_image" = "sprit.png";
    "category_name" = "Non Alcoholic Bevereges";
    sequance = 21;
    status = 0;
    "store_id" = 1;
},
    {
    "added_by" = 1;
    "category_id" = 162;
    "category_image" = "nonalcoholic.png";
    "category_name" = "Non Alcoholic Coolers";
    sequance = 22;
    status = 0;
    "store_id" = 1;
}

this is nslog of server reply

This is the slog of the array in which I have stored the data

2017-06-13 10:16:39.181 MenuBar[1703:94836] (
"kinfisherultra.png",
"impotedbeernew.png",
"blendedwhiskyne.png",
"johywalkerbluee.png",
"singlemaltnew.png",
"americanwishkynew.png",
"irishwishkynew.png",
"Belvedere-Vodka.png",
"Ginnew.png",
"Tequilanew.png",
"rumnew.png",
"amarula.png",
"aprities.png",
"breezernew1.png",
"conganbrndynew.png",
"wine&sparkling.png",
"bottels.png",
"cocktailpitchers.png",
"mocktails.png",
"shots.png",
"classiccocktail.png",
"sprit.png",
"nonalcoholic.png"

)

and I have a base url how to get all these images in collection view?

this is customcell.h file

#import <UIKit/UIKit.h>

@interface CustomCell : UICollectionViewCell


@property (strong, nonatomic) IBOutlet UIImageView *imageView;
@property (strong, nonatomic) IBOutlet UILabel *lbl;



@end

customcell.h file

#import "CustomCell.h"

@implementation CustomCell


-(void)awakeFromNib {
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
[tap setNumberOfTapsRequired:1];
[self addGestureRecognizer:tap];
[super awakeFromNib];
}

-(void)onButtonTapped:(id)sender
{
//the response to the gesture.
//mind that this is done in the cell. If you don't want things to happen from this cell.
//then you can still activate this the way you did in your question.

}
@end

view controller.hfile

 if(!sharedSessionMainQueue){
    sharedSessionMainQueue = [NSURLSession sessionWithConfiguration:nil delegate:nil delegateQueue:[NSOperationQueue mainQueue]];
}

 [[sharedSessionMainQueue dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
    //  NSError *error;
    NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
    NSDictionary *temp=jsonDict;
    NSLog(@"Req cust:%@",requestReply);
    NSLog(@"requestReply cust liqour category: %@", jsonDict);
    NSArray *imgNameArray = [temp valueForKey: @"category_name"];
    NSLog(@"$$$%@$$$",imgNameArray);
        self.iname=[jsonDict valueForKey:@"category_image"];
        NSLog(@"%@",self.iname);

            //[self imagedl];
    NSDictionary *tempz=[jsonDict valueForKey:@"category_name"];
            Photos = [NSArray arrayWithObjects:@"http://test.kre8tives.com/barebones/upload/%@",self.iname, nil];
    NSLog(@"^^^%@",tempz);


        //NSLog(@"$$%@",[Photos objectAtIndex:]); //Here can show Img's values correctl
   // }
   // self.recipeImageView.image = [UIImage imageNamed:self.recipeImageName];
}] resume];

    _barbutton.target = self.revealViewController;
    _barbutton.action = @selector(revealToggle:);
    //[self.view addGestureRecognizer:self.revealViewController.panGestureRecognizer];
self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"background"]];
    // Do any additional setup after loading the view, typically from a nib.
 }

-(void)geti{
    NSString  *temps= [NSString stringWithFormat:@"http://test.kre8tives.com/barebon/upload/%@",self.iname];
UIImage *temp;
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:temps]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSLog(@"%@",response);
     //temp.image= [UIImage imageWithData:data];
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return 23;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//[CustomCell registerClass:[CustomCell class] forCellWithReuseIdentifier:reuseIdentifier];


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

[[[cell contentView] subviews] makeObjectsPerformSelector:@selector(removeFromSuperview)];
UIView * contents=[[UIView alloc]      initWithFrame:cell.contentView.bounds];
[contents setBackgroundColor:[UIColor clearColor]];
[cell.contentView addSubview:contents];

_imageView.image;

// set tag to the indexPath.row so we can access it later
[cell setTag:indexPath.row];

// add interactivity
UITapGestureRecognizer * tap=[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(onButtonTapped:)];
[tap setNumberOfTapsRequired:1];
[cell addGestureRecognizer:tap];

NSString *fileName = [NSString stringWithFormat:@"%@",self.iname]; //objectAtIndex:indexPath];
NSLog(@"%@",fileName);
NSString *baseurl=[NSString stringWithFormat:@"http://test.kre8tives.com/barebon/upload/"];
NSDictionary *doors = [NSMutableArray new];
NSString *base=[NSString stringWithFormat:@"http://test.kre8tives.com/barebon/upload/"];

    NSLog(@"%@",doors);

NSString *path = [NSString stringWithFormat:@"%@/%@", baseurl, _iname];
NSURL *url = [NSURL URLWithString:path];
NSData *data = [NSData dataWithContentsOfURL:url];
UIImage *imgage = [[UIImage alloc] initWithData:data];
//NSString *filePath = [baseurl stringByAppendingPathComponent:fileName];
NSDictionary *dict = self.iname[indexPath.row];
NSLog(@"%@", [self.iname objectAtIndex: indexPath.row]);


NSString *filePath=[NSString stringWithFormat:@"%@%@",baseurl,fileName];
NSString *paths = [NSString stringWithFormat:@"%@/%@", baseurl, [[dict valueForKey:@"category_image"] objectAtIndex: indexPath.row]];
NSLog(@"%@",dict);
NSString  *temps= [NSString stringWithFormat:@"%@",filePath];
[NSURLConnection sendAsynchronousRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:paths]] queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    NSLog(@"%@",response);

    UIImage *imgage = [[UIImage alloc] initWithData:data];
    _imageView.image=[[UIImage alloc] initWithData:imgage];

    _imageView.image=[[NSData alloc]initWithData:data];
    //imageView.contentMode = UIViewContentModeScaleAspectFill;
    // _imageView.clipsToBounds = YES;
    //imageView.tag = IMAGE_VIEW_TAG;
}];


if (cell.selected) {
    cell.backgroundColor = [UIColor blueColor]; // highlight selection
}
else
{

    cell.backgroundColor = [UIColor redColor]; // Default color
}
return cell;


}
Akshay
  • 179
  • 1
  • 16

2 Answers2

0
    NSString *path = [NSString stringWithFormat:@"%@/%@", baseUrl, imageName];
    NSURL *url = [NSURL URLWithString:path];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *imgage = [[UIImage alloc] initWithData:data];

pass this image to your imageview. As you are loading images from server, you can also use SDWebImage for image caching purpose. https://github.com/rs/SDWebImage

Developer
  • 822
  • 9
  • 23
  • but how there is a array of image name separately!! – Akshay Jun 13 '17 at 05:00
  • I ll try and let u know – Akshay Jun 13 '17 at 05:00
  • NSString *path = [NSString stringWithFormat:@"%@/%@", baseUrl, [[yourImageArray valueForKey:@"category_image"] objectAtIndex: indexPath.row]]; – Developer Jun 13 '17 at 05:02
  • 2017-06-13 10:36:09.619 MenuBar[1764:104943] { URL: http://test.kre8tives.com/barebon/upload//(null) } { status code: 404, headers { "Content-Encoding" = gzip; "Content-Length" = 256; "Content-Type" = "text/html; charset=iso-8859-1"; Date = "Tue, 13 Jun 2017 05:06:08 GMT"; Server = "Apache Phusion_Passenger/4.0.10 mod_bwlimited/1.4 mod_fcgid/2.3.9"; Vary = "Accept-Encoding"; } } – Akshay Jun 13 '17 at 05:06
  • this is my response – Akshay Jun 13 '17 at 05:06
  • https://stackoverflow.com/questions/44489660/custom-uitableviewcell-created-from-xib-doesnt-show/44491440#44491440 Checkout my answer on this link for your first query. – Developer Jun 13 '17 at 05:07
  • @Akshay Can you please provide me your base url?? – Developer Jun 13 '17 at 05:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/146474/discussion-between-coder-and-akshay). – Developer Jun 13 '17 at 05:08
  • http://test.kre8tives.com/barebon/upload/kinfisherultra.png Why are you appending null value ??... Append image name from the array in which you have stored the data – Developer Jun 13 '17 at 05:11
0

Register your custom cell in viewDidLoad method

[yourCollectionView registerNib:[UINib nibWithNibName:@"CustomCell" bundle:nil] forCellWithReuseIdentifier:@"your Cell Identifier"];

And then you will write on cellForItemAtIndexPath method

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {

    static NSString *cellIdentifier = @"your Cell Identifier";
    CustomCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];

    NSDictionary *dict = imageAry[indexPath.row];
    //SdWebimage to load image 
    [cell.imgUser sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", baseUrl, dict[@"imageName"]]];


    return cell;
}

Download SDWebimageImageLoader to use of async image downloader with cache support.

Rahul Mayani
  • 3,761
  • 4
  • 25
  • 40