I am trying to grab images from external API and bind it to my UICollectionView & UIImageView cell with in that View. I am able to get the data and print it in the log file. However, I am not able to see the images on my UICollectionView. Here is the code to my data bindings.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
ImagesViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
// imagesArray is an array with serialized json data.
NSDictionary *finalImages = [self.imagesArray objectAtIndex:indexPath.row];
NSLog(#"Entering collection view.....");
[[cell imageViewCell]setImage:[UIImage imageNamed:[finalImages valueForKey:#"link"]]];
return cell;
}
The is data is coming in JSON format.
data
{
abc: 'abc',
xyz: 'xyx',
link: 'link to an online image'
}
imageNamed is a method to get image from a image file in your bundle (image.png).
In the case you want to get image from URL, download it as Mr Richhard Brown answer or use SDWebImage(set directly with imageView)
Sample colectionView SDWebImage code(nonARC): https://github.com/lequysang/github_zip/blob/master/CollectionViewNonARC.zip
UIImage imageNamed: takes a filename, not a URL.
You need to manually load the image file into an NSData object before you can display it.
NSData dataWithContentsOfURL will do this for you.
UIImage *image = [UIImage imageWithData: [NSData dataWithContentsOfURL:[finalImages valueForKey:#"link"]]];
Related
I have to make a photo gallery similar to the photos app in iPhone. I have included a collection view to display images. When I click insert image button, the saved photos album of Photos app is displayed(using UIImagePicker). But I cannot load the image back to the custom collectionViewCell. The CollectionViewCell contains an UIImageView.
You cellForItemAtIndexPath for collectionView should look like this.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
//create cel...
UICollectionViewCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:#"cellIdentifier" forIndexPath:indexPath];
//create UIImageView (declare imageView in header file)
self.cellImageView = [[UIImageView alloc] initWithFrame:"YOUR FRAME"];
//set image. delete this line if there's no before picking image in uipickercontroller
[self.cellImageView setImage:"YOUR IMAGE BEFORE USING IMAGEPICKER"];
[cell addSubView:self.cellImageView];
return cell;
}
You didFinishPickingMediaWithInfo should look like this...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
UIImage *chosenImage = info[UIImagePickerControllerEditedImage];
//set image when image is chosen from image picker controller
self.cellImageView = chosenImage;
//reload collectionView
[self.collectionView reloadData];
[picker dismissViewControllerAnimated:YES completion:NULL];
}
Please use ALAsset library.
An ALAsset object represents a photo or a video managed by the Photo application.
Please take a look this tutorial series “Creating An Image Gallery Like Over”.
In this tutorial, you will learn how to actually display the saved photo albums in the UICollectionView.
I am working on lazy load of images for UICollectionView. The Image are showing but when I scroll the UICollectionView, images change their positions and there are repetition of images where all the images should be unique. Here is my code:
- (gridcell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
gridcell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"gallerycollection" forIndexPath:indexPath];
NSDictionary *appsdict = [array_grid objectAtIndex:indexPath.item];
cell.image_grid.image=[UIImage imageNamed:#"griddummyloading"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
NSURL *imageURL = [NSURL URLWithString:[appsdict objectForKey:#"iosthumbnail"]];
NSData *imageData = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:imageData];
dispatch_async(dispatch_get_main_queue(), ^{
cell.image_grid.image=image;
});
});
[collectionview_grid setAllowsSelection:YES];
return cell;
}
I suggest you use a third party library to lazy load images, like this one, which also supports web caching.
Also, consider the conventions used in objective-c:
- avoid snake_casing, use camelCase
- make extensive use of properties
- use upper cased class names
Hey theres a very good example given at Apple developer site.
This is an example code where apple demonstrates a way of doing lazy loading in a table.. referring to this might help you out in some way.
All the best
Currently I am loading all of my UITableViewControllers with images and text. I'm not sure if there is a way of shortening my loading times. I'm thinking that GCD might be the best route to go, however, I'm not too sure that I'm using this correctly:
dispatch_async(dispatch_get_main_queue(), ^{
[some method];
});
This is being loaded in the ViewDidLoad, and I'm unsure if this is the correct place to use GCD. Also, is this correct way of asynchronously loading information?
use this, it downloads multiple files in different threads.And its asynchronous.
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//load image here
});
If you want to load all images ,even the images which might be very down in tableView (say image at 20th row).Then viewDidLoad is perfect approach, or if you want to only load images that are currently to be shown, then download images in cellForRow:atIndexPath method.
OR,you can use AFNetworking to download multiple files much efficiently.
Hope this helps
For loading images faster in table view cells you can use SDWebImage.
It is very simple to use. Just import the SDWebImage folder into your Xcode project. It contains a category on UIImageVIew. So on any imageView object just call the method setImageWithURL as illustrated below:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *MyIdentifier = #"MyIdentifier";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
if (cell == nil)
{
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
reuseIdentifier:MyIdentifier] autorelease];
}
// Here we use the new provided setImageWithURL: method to load the web image
[cell.imageView setImageWithURL:[NSURL URLWithString:#"http://www.domain.com/path/to/image.jpg"]
placeholderImage:[UIImage imageNamed:#"placeholder.png"]];
cell.textLabel.text = #"My Text";
return cell;
}
I am just a beginner. i want to display image which is stored in database in a collection view cell. I have already created database and collection view. my code is as follow,
MyDatabase *data;
data=[MyDatabase new];
imagearray=[data OpenMyDatabase:#"SELECT pic_name FROM exterior" :#"pic_name"];
so my question is how can i display images ? let me know the way/code
thanks in advance
You can use following code for display UIImage grid. Add UICollectionView for your xib file. Don't forget to set the delegate in collection.
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return noOfItem/ noOfSection;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView
numberOfItemsInSection:(NSInteger)section
{
return noOfSection;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView
cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *identifier = #"Cell";
UICollectionViewCell *cell =
[collectionView dequeueReusableCellWithReuseIdentifier:identifier
forIndexPath:indexPath];
UIImageView *recipeImageView = (UIImageView *)[cell viewWithTag:100];
recipeImageView.image = [imageArray objectAtIndex:
(indexPath.section * noOfSection + indexPath.row)];
return cell;
}
In your xib file add UIImageView to your CollectionViewCell and change it tag value to 100.
Please go through below links.
http://developer.apple.com/library/ios/#documentation/UIKit/Reference/UICollectionView_class/Reference/Reference.html
Above link is from Apple developer site. It has all details of UIcollectionview and tutorials related it. Below URL is also having sample tutorial, which will help you a lot.
http://www.raywenderlich.com/22324/beginning-uicollectionview-in-ios-6-part-12
You have images array retrieved from database. Now it will act as data source for collection view. You need to form UICollectionViewCell which will have those images accordingly. Please study above links, you will get to know.
The UITableViewCell has UIImage to be displayed within it.
Images are obtained from the server, and stored in NSDictionary as NSData.
This image is resized for non-retina devices using UIImageGraphicsBeginImageContext as listed below.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellID=#"Cell";
UITableViewCell *cell=[tableView dequeueReusableCellWithIdentifier:cellID];
NSDictionary *myDictionary=[self.tableObject objectAtIndex:indexPath.row];
NSData *imgData=[myDictionary objectForKey:#"icon"];
UIImage *img=[UIImage imageWithData:imgData];
if(isRetina]){
cell.iconImageView.image=img;
}else{
CGRect imgRect=CGRectMake(0, 0, img.size.width/2.0, img.size.height/2.0);
UIGraphicsBeginImageContext(imgRect.size);
[img drawInRect:imgRect];
UIImage *newImg=UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
cell.iconImageView.image=newImg;
}
}
Would it be better approach and less memory intensive or should store it in the disk, and then access the image from it and assign it to cell.iconImageView.image;
write it to disk when you download it.
that way, os can lazy read the data, cache it and you dont have the image data in memory all the time