How to add image into collection view cell from saved photo albums? - ios

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.

Related

Loading images in collectionView

Each UITableViewCell contains a UICollectionView. Having trouble figuring out why some UICollectionViewCell images are not showing up. I'm using SDWebImage library to load images. The method - (void)sd_setImageWithURL:(NSURL *)urlcalls [self sd_cancelCurrentImageLoad];. I think cancelling the previous request is the problem, but read somewhere that will create a race condition. Problem doesnt happen for every UITableViewCell, and if I tap on cell for details and go back, the images fill in. Perhaps I need to reload data somewhere. Any tips on how to solve this?
if you try to image fatch from server to try this.
#import "UIImageView+WebCache.h"
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
NewProfileCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:#"Cell" forIndexPath:indexPath];
[cell.img_user_image sd_setImageWithURL:[NSURL URLWithString:[mutDict valueForKey:#"UserImageURL"][indexPath.row]] placeholderImage:[UIImage imageNamed:#"No_IMAGE_PLACE"]];
return cell;
}
Try it:
- (void)prepareForReuse
{
[super prepareForReuse];
[self.imageView cancelCurrentImageLoad];
[self.imageView setImage:[UIImage imageNamed:#""] forState:UIControlStateNormal]; // Placeholder image
}

UICollectionView showing wrong layer when scrolling

I know it's pretty common question. I've seen number of people ask here this type of a question, but I still can't seem to get it right.
I have a UICollectionView with pictures. I'm the getting the images from facebook so I'm using SDWebImage to load them.
Until here everything works ok.
The user can upload pictures that he took. so until the uploading process is finished and image was uploaded to facebook, I want the local image to be shown but with a layer (different alpha, activity etc') and once i get a notification from the server, the new image from facebook is replacing the local one.
My problem is that after adding the local images to the collectioView with the layer, more cells are displaying this layer and from time to time when i scroll up , the new images changes.
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
PictureCell* cell = (PictureCell *)[_collectionView dequeueReusableCellWithReuseIdentifier:[PictureCell reuseIdentifier] forIndexPath:indexPath];
Picture *picture=(Picture *)[_campaign.pictures objectAtIndex:indexPath.row];
//If the picture is being uploaded, add the image from the local image property and set the blur layer
if (!picture.facebookPicture) {
cell.pictureImageView.image = picture.image;
[cell TaggleBlur:YES];
}
else
{
[cell.pictureImageView sd_setImageWithURL: [NSURL URLWithString:picture.facebookPicture.source ]placeholderImage:[UIImage imageNamed:#"actionBar_pico_icon"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
NSArray *visibleIndexPaths = [_collectionView indexPathsForVisibleItems];
if (error==nil) {
if ([visibleIndexPaths containsObject:indexPath]) {
cell.pictureImageView.image = image;
[cell TaggleBlur:NO];
}
}
}];
}
return cell;
}

Binding UICollectionView with rest api data issue

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"]]];

xcode how to display images in collection view cell from database

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.

Set iOS Address Book Image in UITableViewCell

I am trying to integrate the AddressBookUI API into my iOS 5 app to display selected content in a table view. I have been able to implement the AddressBook Picker and set it so when a user selects a person from their address book, it populates the label of the cell in the TableView. I would also like it to be able to display the image of the selected person in the same cell if one exists and a default missing picture image if there is not one.
I can't see to get my head around how to store both the name and the image data in the same TableView cell.
Anyone have any suggestions of how I might accomplish this. I have read the developer docs and know i should be using the ABPersonCopyImageDataWithFormat command. I just can't seem to get it to implement into the tableview cell.
Here are the snippets of code I have so far:
// Store the name into memory when the user selects, then dismiss the view.
- (BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
{
NSString *selectedPerson = (__bridge NSString *)ABRecordCopyCompositeName(person);
[people insertObject:selectedPerson atIndex:0];
if (ABPersonHasImageData(person) == TRUE) {
NSLog(#"Person has an image!");
} else {
NSLog(#"Person does not have an image.");
}
[self dismissModalViewControllerAnimated:YES];
[self.tableView reloadData];
return NO;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
PartyCell *cell = (PartyCell *)[tableView dequeueReusableCellWithIdentifier:#"Cell"];
cell.backgroundView = [[GradientView alloc] init];
cell.personLabel.text = [people objectAtIndex:indexPath.row];
cell.personImage.image = [UIImage imageNamed:#"missing.png"]; // THIS NEEDS TO DISPLAY THE SELECTED USERS PICTURE. CURRENTLY SHOWS A DEFAULT USER.
return cell;
}
Thanks!
Found a simple solution: store the image of the selected contact in a new NSMutableArray at index 0 each time.
UIImage *image =[UIImage imageWithData:(__bridge NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
[peopleImage insertObject:image atIndex:0];
The images can then be loaded like normal into a cell by calling the array in the UITabelView
cell.personImage.image = [peopleImage objectAtIndex:indexPath.row];
First, you get a UIImage from the ABRecordRef:
[UIImage imageWithData:(NSData *)ABPersonCopyImageDataWithFormat(person, kABPersonImageFormatThumbnail)];
Then, you can set the UITableViewCell's imageView's image like:
[[cell imageView] setImage: image];
Couple other links that may help you:
http://www.chrisdanielson.com/tag/uitableviewcell/
http://goddess-gate.com/dc2/index.php/post/421

Resources