Loading url image array add to UIcollectionview - objective-c

I want to load images array from Url and add to UICollectionView
Tony.pageImages =imagearray;
[Tony reloadData];
NSLog(#"imagearray%#",imagearray);
Tony is Collection view.h/m files. pageImages is NSMutableArray on the Tony.h files. The Image array display console log mean i got image array correctly
Those are Url.
but when i add code on the UICollectionView method it doesn't work
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
UIImageView *CollectionViewImage = (UIImageView *)[cell viewWithTag:120];
CollectionViewImage.image= [UIImage imageNamed:self.pageImages[indexPath.row]];
return cell;
}
Do i miss something ?
This is my imagearray
imagearray = [[NSMutableArray alloc]init];
for (int i = 0;i < comment.count; i++)
{
[imagearray addObject:comment[i][#"pic_url"]] ;
}

You have to implement and set the following dataSource delegate UICollectionViewDataSource in your viewcontroller:
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section {
return imagearray.count;
}

If I got it right, you need to load image from URL.
To do that simply add this code:
[CollectionViewImage.image setImageWithURL:[NSURL URLWithString:self.pageImages[indexPath.row]] placeholderImage:nil];
Or if you want to add placeholder image, just set placeholderImage:[UIImage imageNamed:#"someImageInYourAssets"];

NSData * imageData = [[NSData alloc] initWithContentsOfURL: [NSURL URLWithString:self.pageImages[indexPath.row]]];
UIImage *image = [UIImage imageWithData: imageData];
CollectionViewImage.image = image;
This is solution how solve problem loading image from url,without addition frameworks such as "Heneke","AFNetworking"..etc..In Objective C Image View can't communicated with URL directly, but can using NSData as Helper Object(wrapper).

Related

Objective C model - How to download image inside a model class?

I have the following model of a box, it is meant to download images in a background thread and create an image from this downloaded image.
The viewcontroller has a custom uicollectioncell, but its just a uiimageview; nothing too complex.
In the cellForItemAtIndexPath I want to assign the cell's imageview using the model's downloaded image.
However, its not quite working;
The image never appears
If I move the background image downloader to the cellForItemAtIndexPath and change a few items then the image loads fine.
But what I'm wanting is to seperate the ViewContoller and the model; the model should do the heavy lifting and the viewcontroller simply handles the display.
Code follows
// ViewController: View Did Load
- (void)viewDidLoad {
[super viewDidLoad];
if (!self.picturesArray) self.picturesArray = [NSMutableArray arrayWithCapacity:kNumberOfCells];
self.collectionView.delegate = self;
self.collectionView.dataSource = self;
self.collectionView.backgroundColor = [UIColor clearColor];
for (int i=0; i<kNumberOfCells; i++)
{
Box *box = [[Box alloc] init];
[self.picturesArray addObject:box];
box = nil;
}
}
// ViewController : collectionView delegage
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
Box *box = self.picturesArray[indexPath.row];
cell.backgroundColor = box.bgColor;
cell.imageView.image = box.image;
return cell;
}
The box model is as follows
// Box model with an image property
- (instancetype)init
{
self = [super init];
if (self)
{
NSURL *url = [NSURL URLWithString:kUrlString];
// Block variable to be assigned in block.
__block NSData *imageData;
dispatch_queue_t backgroundQueue = dispatch_queue_create("imagegrabber.bgqueue", NULL);
// Dispatch a background thread for download
dispatch_async(backgroundQueue, ^(void) {
imageData = [NSData dataWithContentsOfURL:url];
if (imageData.length >0)
{
// self.image is a property of the box model
self.image = [[UIImage alloc] initWithData:imageData];
// Update UI on main thread
dispatch_async(dispatch_get_main_queue(), ^(void) {
});
}
});
}
return self;
}
My question is this:
How do I get the box model to download the image and then in my cellAtIndexPath make the cell's imageView assign its image from that downloaded boxmodel image?
A further unrelated question
Isn't it best practice to seperate the model from the actual downloading of items? But if I'm not meant to put this in the view controller, and not the model where does it go and how/whem would you call it?
Thanks for now
Separating the model from its image file and keeping images in a cache with a key which is model's image URL, is better approach.
Also, putting initialization method in your MyCustomCollectionViewCell is better way to configure your cell.
I suggest you to use https://github.com/rs/SDWebImage (or similar libraries) to download and cache images.
MyCustomCollectionViewCell.h :
#interface MyCustomCollectionViewCell : UITableViewCell
-(void) initializeWithBox:(Box*)box;
MyCustomCollectionViewCell.m with SDWebImage:
#implementation MyCustomCollectionViewCell
-(void) initializeWithBox:(Box*)box
{
[self setBackgroundColor:[box bgColor]];
[self.imageView sd_setImageWithURL:[NSURL URLWithString:[box kUrlString]] placeholderImage:nil];
}
MyCustomCollectionViewCell.m without libraries:
#implementation MyCustomCollectionViewCell
-(void) initializeWithBox:(Box*)box
{
[self setBackgroundColor:[box bgColor]];
__weak typeof(self) weakSelf = self;
NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithURL:[NSURL URLWithString:[box kUrlString]] completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
if (data) {
UIImage *image = [UIImage imageWithData:data];
if (image) {
dispatch_async(dispatch_get_main_queue(), ^{
typeof(self) strongSelf = weakself;
if(strongSelf)
[self.imageView setImage:image];
});
}
}
}];
[task resume];
}
In your view controller:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MyCustomCollectionViewCell *cell = (MyCustomCollectionViewCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellId forIndexPath:indexPath];
Box *box = self.picturesArray[indexPath.row];
[cell initializeWithBox:box]
return cell;
}

Objective C UI Collection issue

Description :-
Hello , When we are running this code when is cell relocate image is changing on scrolling.
Please let me know how to fix.
- (UICollectionViewCell )collectionView:(UICollectionView )collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cellidentifier";
CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSString * string = [_temp objectAtIndex:indexPath.row];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
// retrive image on global queue
UIImage * img = [UIImage imageWithData:[NSData dataWithContentsOfURL: [NSURL URLWithString:string]]];
dispatch_async(dispatch_get_main_queue(), ^{
cell.imageView.image = img;
});
});
return cell;
}
The problem here is that if the cells get reused the previous image might still be loading or be set. You can override the cell's prepareForReuse to nil the image. For asynchronous loading of images you should check if the cell's model object is stil the same as when you started the loading, if not, then don't set the image as it arrived too late.

UICollectionView cell imageview not show image, but it's show background color xcode [duplicate]

I have an issue with the images not showing, even though they are loading. I have tried this several different ways and the same result... no images. I do get white rectangles that are the size and color specified in the Storyboard. I get the correct number of rectangles in the popover. The log shows the names and ids correctly.
If I call the array directly, I get the same result... white rectangles.
My target is iOS7. Running Xcode 5.0.2. Images are coming from a SQL database. This is a live app, which I am updating to full iOS7 and where I am swapping out a custom grid layout for UICollectionView. Using a CollectionViewController, embedded into a NavigationController, which is accessed via a UIButton. As you can see, there is no custom cell. The images are to show in a popover and are then selectable by the user to change the background of the underlying view.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
NSLog(#"Filename: %#", tempImage);
NSLog(#"ButtonTagNumber: %#", buttonTagNumber);
UIImageView *image = [[UIImageView alloc]init];
image.image = [UIImage imageNamed:tempImage];
NSLog(#"Image.image: %#", image.image);
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The fix which includes actually naming the BackgroundCell (duh) in the cellForItemAtIndexPath method and creating a small method in the BackgroundCell controller to set the image.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BackgroundCell *cell = (BackgroundCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
[cell setImage:[UIImage imageNamed:tempImage]];
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The main cell code;
- (id)initWithFrame:(CGRect)frame
{
_backgroundImage = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_backgroundImage];
if (self) {
// Initialization code
}
return self;
}
-(void)setImage:(UIImage *)image
{
_backgroundImage.image = image;
}
The problem is that the image is not being set in the cell's view hierarchy. To do that, subclass UICollectionViewCell and create a imageView property in the subclass:
#interface CollectionViewCellSubclass : UICollectionViewCell
#property (nonatomic, strong) UIImageView *imageView;
#end
...and init the UIImageView in its initWithFrame:
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
Set this subclass as the Custom Class for the cell in storyboard and then load it in the collectionView:cellForItemAtIndexPath method above setting the image to the cell's image subview:
CollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
//...
cell.imageView.image = [UIImage imageNamed:tempImage];
Hope this helps.
Adding cell.contentView.frame = [cell bounds] after the line dequeueReusableCellWithReuseIdentifier worked for me.
Suppose you add a custom (200,200) UICollectionViewCell through xib and change its frame somewhere in the code to be e.g. (120,120). You have to set its content view’s frame accordingly, otherwise image on its imageView (which is on its content view) will not be shown properly.

UICollectionView images not showing

I have an issue with the images not showing, even though they are loading. I have tried this several different ways and the same result... no images. I do get white rectangles that are the size and color specified in the Storyboard. I get the correct number of rectangles in the popover. The log shows the names and ids correctly.
If I call the array directly, I get the same result... white rectangles.
My target is iOS7. Running Xcode 5.0.2. Images are coming from a SQL database. This is a live app, which I am updating to full iOS7 and where I am swapping out a custom grid layout for UICollectionView. Using a CollectionViewController, embedded into a NavigationController, which is accessed via a UIButton. As you can see, there is no custom cell. The images are to show in a popover and are then selectable by the user to change the background of the underlying view.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
NSLog(#"Filename: %#", tempImage);
NSLog(#"ButtonTagNumber: %#", buttonTagNumber);
UIImageView *image = [[UIImageView alloc]init];
image.image = [UIImage imageNamed:tempImage];
NSLog(#"Image.image: %#", image.image);
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The fix which includes actually naming the BackgroundCell (duh) in the cellForItemAtIndexPath method and creating a small method in the BackgroundCell controller to set the image.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
BackgroundCell *cell = (BackgroundCell *)[collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
NSDictionary * tempDict = [_arrayOfBckgnds objectAtIndex:indexPath.row];
NSNumber *buttonTagNumber = (NSNumber *)[tempDict objectForKey:#"id"];
int buttonTag = [buttonTagNumber intValue];
NSString *tempImage = [tempDict objectForKey:#"fileName"];
[cell setImage:[UIImage imageNamed:tempImage]];
// needed for selecting the background in didSelectItemAtIndexPath
_bgtag = buttonTag;
return cell;
}
The main cell code;
- (id)initWithFrame:(CGRect)frame
{
_backgroundImage = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_backgroundImage];
if (self) {
// Initialization code
}
return self;
}
-(void)setImage:(UIImage *)image
{
_backgroundImage.image = image;
}
The problem is that the image is not being set in the cell's view hierarchy. To do that, subclass UICollectionViewCell and create a imageView property in the subclass:
#interface CollectionViewCellSubclass : UICollectionViewCell
#property (nonatomic, strong) UIImageView *imageView;
#end
...and init the UIImageView in its initWithFrame:
_imageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
[self.contentView addSubview:_imageView];
Set this subclass as the Custom Class for the cell in storyboard and then load it in the collectionView:cellForItemAtIndexPath method above setting the image to the cell's image subview:
CollectionViewCellSubclass *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"BckgndCell" forIndexPath:indexPath];
//...
cell.imageView.image = [UIImage imageNamed:tempImage];
Hope this helps.
Adding cell.contentView.frame = [cell bounds] after the line dequeueReusableCellWithReuseIdentifier worked for me.
Suppose you add a custom (200,200) UICollectionViewCell through xib and change its frame somewhere in the code to be e.g. (120,120). You have to set its content view’s frame accordingly, otherwise image on its imageView (which is on its content view) will not be shown properly.

IOS: LazyLoad image From Json

i'm new on this programming language... i try to consume json from some servers, after success consume all information, now i've problem tho show "lazyload" image into tableviewcell
i use AsyncImageView to make it happen, and here my code
-(UITableViewCell *)tableView :(UITableView *) tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NetraCell *cell =(NetraCell *)[tableView dequeueReusableCellWithIdentifier:#"cell"];
if (cell==nil){
cell=[[NetraCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:#"cell"];
//common settings
cell.imageView.contentMode = UIViewContentModeScaleAspectFill;
cell.imageView.frame = CGRectMake(0.0f, 0.0f, 44.0f, 44.0f);
cell.imageView.clipsToBounds = YES;
///get url image from url
//NSString *path=[[[Deals_array objectAtIndex:indexPath.row] objectForKey:#"image"]objectForKey:#"thumbnail"];
////convert image into NSDATA
// NSData* imageData = [NSData dataWithContentsOfURL: [NSURL URLWithString: path]];
////read NSData Image
// UIImage* image = [UIImage imageWithData: imageData];
//////set rounded imager with NetraImage
// image=[ImageManipulator makeRoundCornerImage:image : 30 : 30];
////show image into UItableviewCell
// cell.imageView.image=image;
}
else{
//cancel loading previous image for cell
[[AsyncImageLoader sharedLoader] cancelLoadingImagesForTarget:cell.imageView];
}
cell.imageView.image = [UIImage imageNamed:#"Placeholder.png"];
cell.imageView.imageURL = [[[imageURLs objectAtIndex:indexPath.row]objectForKey:#"image"]objectForKey:#"thumbnail"];
NSString *price=[[[somearray objectAtIndex:indexPath.row] objectForKey:#"price"]objectForKey:#"formatted"];
///call image from JSOn
///draw to tableviewcell
if(price!=NULL){
cell.detailTextLabel.text= [[[Deals_array objectAtIndex:indexPath.row] objectForKey:#"price"]objectForKey:#"formatted"]; //1 draw the price
//[cell release]
}
else{
cell.detailTextLabel.text= #"Call";
}
//draw headline
cell.textLabel.text=[[Deals_array objectAtIndex:indexPath.row] objectForKey:#"headline"];
///draw image
[UIApplication sharedApplication].networkActivityIndicatorVisible=NO;
return cell;
}
this line is my path of immage
//NSString *path=[[[Deals_array objectAtIndex:indexPath.row] objectForKey:#"image"]objectForKey:#"thumbnail"];
and this is the default method to assign pic into uitableviewcell
cell.imageView.imageURL = [imageURLs objectAtIndex:indexPath.row];
but when i try this method
cell.imageView.imageURL = [[[imageURLs objectAtIndex:indexPath.row]objectForKey:#"image"]objectForKey:#"thumbnail"];
its error, the main question is
why error? is that my remote image path wrong? or my method wrong
Need your help please :) many thanks
once check for whether you are assigning appropriate imageurl to cell image url or not in your code,[[[imageURLs objectAtIndex:indexPath.row]objectForKey:#"image"]objectForKey:#"thumbnail"], and go through this,it may help for you
lazy loading of images in UITableView cell