Load image into UICollectionVIewCell - ios

Im having problem setting image into UiCollectionViewCell
THis is my code :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NSURL * imageURL = [NSURL URLWithString:#"https://mozorg.cdn.mozilla.net/media/img/firefox/firstrun/logo.png?2013-06"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image];
myImageView.image = [UIImage imageNamed:#"myimg"];//set the image
GLCell *cell = (GLCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
cell.displayString = [NSString stringWithFormat:#"%d user number", indexPath.row];
cell.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:#"myimg"]];
return cell;
}
the string value shows without problem , but cell have no image.

NSURL * imageURL = [NSURL URLWithString:#"https://mozorg.cdn.mozilla.net/media/img/firefox/firstrun/logo.png?2013-06"];
NSData * imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage * image = [UIImage imageWithData:imageData];
GLCell *cell = (GLCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CELL_IDENTIFIER forIndexPath:indexPath];
cell.displayString = [NSString stringWithFormat:#"%d user number", indexPath.row];
cell.backgroundColor = [UIColor colorWithPatternImage:image]];

You aren't adding the myImageView to your cell. Instead of creating your own UIImageView you could just use the UITableViewCell's imageView property

Related

Uilabel is not visible in collectionview

Im using the following code to load images from url into collection view
-
(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
UILabel *recipeLabel = (UILabel *)[cell viewWithTag:200];
if ([ImageArray count] >0){
for(int i = 0; i < [ImageArray count]; i++)
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void) {
NSData *data0 = [NSData dataWithContentsOfURL: [NSURL URLWithString:[ImageArray objectAtIndex:indexPath.row]]];
UIImage *image = [UIImage imageWithData: data0];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
recipeImageView.image = image;
});
});
}
}else{
UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 10, cell.bounds.size.width, 40)];
title.text = #"No image record found";
title.tag = 200;
[title setHidden:true];
[cell.contentView addSubview:title];
}
[spinnerShow stopAnimating];
cell.layer.shouldRasterize = YES;
cell.layer.rasterizationScale = [UIScreen mainScreen].scale;
return cell;
}
If the ImageArray has got some url, if loop will be executed. Else the else loop will be executed where it will display a label saying no image record found. But the uilabel is not getting visible
Why are you using for loop in cellForItemAtIndexPath? This method is called for every item in UICollectionView. In else condition, add log or breakpoint and check if it is going there or always going in if condition. Make sure if you need to check for array count or want to check if image exists on URL?

UICollectionView doesn't display any image

I'm trying to display image from documents directory in UICollectionView, everything works fine, but no image appears
- (void)viewDidLoad
{
[super viewDidLoad];
// Initialize recipe image array
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsDirectory error:nil];
NSMutableArray *image = [NSMutableArray array];
for (NSString *tString in dirContents) {
if ([tString hasSuffix:#".png"]) {
[image addObject:tString];
listeImages = [NSArray arrayWithArray:image];
}
}
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
return listeImages.count;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100];
UILabel *LabelView = (UILabel *)[cell viewWithTag:110];
LabelView.text = [listeImages objectAtIndex:indexPath.row];
listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]];
NSLog(#" List UIImageView listeImageView.image %#",listeImageView.image);
cell.backgroundColor = [UIColor redColor];
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
NSLog(#" List NSArray *listeImages %#",listeImages);
cell.tag = indexPath.row;
return cell;
}
with this piece of code, displays the correct UICollectionView UILabel well but no image is displayed.
I use this line of code to store my image in listeImageView.image.
UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100];
listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]];
Here are the results of my NSLog:
2014-10-11 09:46:31.117 imgapp[2803:60b] List NSArray *listeImages; (
"image10102014233607.png",
"image10102014233616.png",
"image10102014233627.png"
)
2014-10-11 09:46:31.158 imgapp[2803:60b] List UIImageView listeImageView.image (null)
2014-10-11 09:46:31.171 imgapp[2803:60b] List UIImageView listeImageView.image (null)
2014-10-11 09:46:31.178 imgapp[2803:60b] List UIImageView listeImageView.image (null)
any help will be appreciated.
Thanks
You have below,
listeImageView.image = [UIImage imageNamed:[listeImages objectAtIndex:indexPath.row]];
But your doing,
cell.backgroundView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:#"photo-frame.png"]];
Why your not using your listeImageView as below:-
cell.backgroundView = listeImageView;
That should get you the image.
UPDATE :-
for (NSString *tString in dirContents) {
if ([tString hasSuffix:#".png"]) {
**[image addObject:[UIImage imageWithContentsOfFile:tString] ]; //Do this as in below line you were not create a image object which you are trying to get.**
//Also check your image array for object, that is it getting stored or not.
// [image addObject:tString];
}
}
listeImages = [NSArray arrayWithArray:image]; //Do this after you have added all objects in your image mutable array and not in for loop as this will initialise it many tiems depending on your loop run count.
You're not adding your listeImageView to anything.
Try it with
[cell addSubview:listeImageView];
or if you really want it to be the backgroundView
cell.backgroundView = listeImageView;
But it'd be much cleaner if you create a custom UICollectionViewCell-class where you can define what content this cell has.
Have u tried to give background color to cell instead of backgroundview.do that once if possible with same code of yours!
Or
You can try like :-UIImageView *listeImageView = (UIImageView *)[cell viewWithTag:100];
[cell bringviewtoFront:listeImageView ];
You'd be much better off subclassing UICollectionViewCell, and setting up the subviews in a storyboard. Your code could be much cleaner this way:
static NSString *kCustomCellIdentifier = #"customcell";
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
MYAPPCustomCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kCustomCellIdentifier forIndexPath:indexPath];
// Load custom image
NSString *imageName = [listeImages objectAtIndex:indexPath.row];
if (imageName)
{
UIImage *image = [UIImage imageNamed:imageName];
if (image)
{
cell.customImageView.image = image;
}
else
{
// Set an empty state here
cell.customImageView.image = [UIImage imageNamed:#"errorImage.png"];
}
}
return cell;
}

I am using tableview to load images from url but image repeating issue in tableview

I am facing image repeating issue, when try to scroll to downside or upside if image not loading completed then image repeat the same as complete the image downloaded. Here is my code:
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellIdentifier = #"galCustom";
GTInstaTableViewCell *cell = (GTInstaTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"GTInstaTableViewCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
for (UIView *view in cell.imageView.subviews)
{
[view removeFromSuperview];
}
cell._imageLoadingView.hidden = NO;
[cell._imageLoadingView startAnimating];
}
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
GTInstaTableViewCell *cell1 = (GTInstaTableViewCell *)[tableView cellForRowAtIndexPath:indexPath];
//Gallery Photo
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),^
{
cell.imageView.image = nil;
NSString *strImgname = [[arrTabledata objectAtIndex:indexPath.row] objectForKey:#"photo_name"];
imgPname = [IMAGE_URL stringByAppendingString:strImgname];
NSString *aPath = [self.thumbDirectory stringByAppendingPathComponent:strImgname];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:aPath])
{
NSURL *url = [NSURL URLWithString:imgPname];
NSData *imgData = [[NSData alloc] initWithContentsOfURL:url];
float containerWidth = 648.0;
//Image Resize
UIImage *image = [UIImage imageWithData:imgData];
float width = image.size.width;
float height= image.size.height;
float thumbWidth = width/containerWidth;
float imageHeight = height/thumbWidth;
CGSize sacleSize = CGSizeMake(containerWidth, imageHeight);
UIGraphicsBeginImageContextWithOptions(sacleSize, NO, 0.0);
[image drawInRect:CGRectMake(0, 0, sacleSize.width, sacleSize.height)];
UIImage *resizedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *imageData = UIImagePNGRepresentation(resizedImage);
[imageData writeToFile:aPath atomically:YES];
}
UIImage *image = [UIImage imageWithContentsOfFile:aPath];
dispatch_async(dispatch_get_main_queue(), ^
{
if (image)
{
cell.imageView.image = image;
cell.imageView.contentMode = UIViewContentModeScaleAspectFit;
cell.imageView.clipsToBounds = YES;
[cell1._imageLoadingView stopAnimating];
cell1._imageLoadingView.hidden = YES;
}
else
{
cell.imageView.image = [UIImage imageNamed:#"noImage.jpg"];
}
});
});
}
You should implement the GTInstaTableViewCell.m method:
- (void) prepareForReuse
{
self.imageView.image = nil;
}

collection view scrolling issue

collection view scrolling issue :
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
photoFuncVal=1;
size=CGSizeMake(85, 85);
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
UIImageView *icon=(UIImageView *)[cell viewWithTag:100];
dispatch_async(kBgQueue, ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[_responseAdvPicArray objectAtIndex:indexPath.row] ]]];
if (imgData)
{
//image = [UIImage imageWithData:imgData];
image = [[UIImage alloc] initWithData:imgData];
//icon.image = image;
scaledImage=[UIImage imageWithData:UIImageJPEGRepresentation(image, 4.0)];
scaledImage=[self imageWithImage:image convertToSize:size];
if (image)
{
dispatch_async(dispatch_get_main_queue(), ^{
//icon.image=scaledImage;
cell.backgroundColor=[UIColor colorWithPatternImage:scaledImage];
});
}
}
});
cell.layer.borderWidth=4;
cell.layer.borderColor=[[UIColor whiteColor]CGColor];
return cell;
}
the problem is that when the collection view is scrolled the images are reloaded and also duplicate at times
Register your cell like this :
static NSString *CellIdentifier = #"CollectionCell";
[self.collectionHome registerNib:[UINib nibWithNibName:#"CollectionViewCell" bundle:nil] forCellWithReuseIdentifier:#"CollectionCell"];
CollectionViewCell *cell ;
if(cell==nil)
{
cell = [cv dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
}
Please try this:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
photoFuncVal=1;
size=CGSizeMake(85, 85);
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
**cell.backgroundColor = nil;**
UIImageView *icon=(UIImageView *)[cell viewWithTag:100];
dispatch_async(kBgQueue, ^{
NSData *imgData = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:#"%#",[_responseAdvPicArray objectAtIndex:indexPath.row] ]]];
if (imgData)
{
//image = [UIImage imageWithData:imgData];
image = [[UIImage alloc] initWithData:imgData];
//icon.image = image;
scaledImage=[UIImage imageWithData:UIImageJPEGRepresentation(image, 4.0)];
scaledImage=[self imageWithImage:image convertToSize:size];
if (image)
{
dispatch_async(dispatch_get_main_queue(), ^{
//icon.image=scaledImage;
cell.backgroundColor=[UIColor colorWithPatternImage:scaledImage];
});
}
}
});
cell.layer.borderWidth=4;
cell.layer.borderColor=[[UIColor whiteColor]CGColor];
return cell;
}

UIImage in UICollectionView not showing

For some reason, I can not get my base 64 encoded image to show in a UIImageView in a UICollectionView.
Here is my code:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
static NSString *identifier = #"task_cell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:identifier forIndexPath:indexPath];
NSDictionary* json = [dataSource objectAtIndex:indexPath.row];
[cell setBackgroundColor:[UIColor whiteColor]];
UIImageView *image = (UIImageView *)[cell viewWithTag:100];
UILabel *text = (UILabel *)[cell viewWithTag:101];
NSString *ext = [[json objectForKey:#"image"] pathExtension];
NSString *final = [NSString stringWithFormat:#"data:image/%#;base64,%#", ext, [json objectForKey:#"b64img"]];
NSURL *url = [NSURL URLWithString:final];
NSData *imageData = [NSData dataWithContentsOfURL:url];
[image setImage:[UIImage imageWithData:imageData]];
text.text = [json objectForKey:#"title"];
return cell;
}
DEBUG LOG:
Printing description of final:
data:image/jpg;base64,/9j/4AAQSkZJRgABAQEASABIAAD/......(goes on for a while)
url = nil
imageData is nil
I do see the UILabel changing.
So, what should I try.
This is likely an issue with how you construct the path to your image. This looks funky:
NSString *final = [NSString stringWithFormat:#"data:image/%#;base64,%#", ext, [json objectForKey:#"b64img"]];
Please post the NSLog value of final, url and whether or not imageData is nil at time of setting image.
I had to do the following to fix it..
Change the code for the url:
NSURL *url = [NSURL URLWithString:[NSString URLEncodeString:final]];
Add the function URLEncodeString to NSString. I used the code from NSURL URLWithString:myString returns Nil?. It works like a charm.
And presto...it works.

Resources