Image on UITableView Cell - ios

i have different folder on document directory and have images on each folder. Now i want to display one image from different folder. i have tried but the program is crashe at the line.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
//I dont know what put here for display from sqlite
NSUInteger row = [indexPath row];
cell.textLabel.text = [array1 objectAtIndex:row];
//cell.detailTextLabel.text = [array2 objectAtIndex:row];
NSString *b = [array2 objectAtIndex:row];
NSLog(#"b=%#",b);
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSLog(#"%#",paths);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSError *error = nil;
NSArray *imageFileNames = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:[documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",b]] error:&error];
NSLog(#"file=%#",imageFileNames);
NSMutableArray *images = [[NSMutableArray alloc ] init];
for (int i = 0; i < [imageFileNames count]; i++)
{
NSString *getImagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:#"%#/%d.png",b, i]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
[images addObject:img];
NSLog(#"%#",getImagePath);
}
NSLog(#"images=%#",images);
cell.imageView.image=[imageFileNames objectAtIndex:0];
//NSLog(#"row=%u",row);
return cell;
}

I think what you are looking at is something like this (please modify to your liking):
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
self.contents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documents error:NULL];
...
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
cell.fileLabel.text = [self.contents objectAtIndex:indexPath.row];
// Build Documents Path with Folder Name
NSString *documents = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *directory = [documents stringByAppendingPathComponent:[self.contents objectAtIndex:indexPath.row]];
NSFileManager *fM = [NSFileManager defaultManager];
// Get Attributes
NSDictionary *fileAttributes = [fM attributesOfItemAtPath:directory error:NULL];
// Check if it's a directory
if ([fileAttributes objectForKey:NSFileType] == NSFileTypeDirectory) {
// Get contents of directory
NSArray *insideDirectory = [fM contentsOfDirectoryAtPath:directory error:NULL];
cell.folderImageView.image = nil;
// Loop through folder contents
for (NSString *file in insideDirectory) {
if ([[[file pathExtension] uppercaseString] isEqualToString:#"PNG"]) {
NSString *imagePath = [directory stringByAppendingPathComponent:file];
UIImage *image = [UIImage imageWithContentsOfFile:imagePath];
cell.imageView.image = image;
break;
}
}
}
return cell;
}

You can directly use [UIImage imageNamed:#""] for images rather than imageWithContentsOfFile

Related

display data from plist to tableCell

i have a pilist whose screen shot is given below:
i want to display its data in table cell. my code is
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
{
return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
NSString *cellIdentifier = #"cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
NSString *path = [[NSBundle mainBundle] pathForResource:#"datalist" ofType:#"plist"];
// Load the file content and read the data into arrays
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
tableData = [dict objectForKey:#"title"];
thumbnails = [dict objectForKey:#"Subtitle"];
cell.textLabel.text = [NSString stringWithFormat:#"%#",thumbnails];
return cell;
}
I am getting this output:
Your code thumbnails = [dict objectForKey:#"Subtitle"]; will return an NSArray.
What you should be doing is iterating through this array and getting each item.
Like cell.textLabel.text = [NSString stringWithFormat:#"%#",[thumbnails objectAtIndex:0]] or cell.textLabel.text = [NSString stringWithFormat:#"%#",[thumbnails objectAtIndex:1]]
Or maybe something like this
cell.textLabel.text = [NSString stringWithFormat:#"%# %#",[thumbnails objectAtIndex:0], [thumbnails objectAtIndex:0]]
Try it.
Try This One:
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSString *plistPath = [documentsPath stringByAppendingPathComponent:#"datalist.plist"];
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
plistPath = [[NSBundle mainBundle] pathForResource:#"datalist" ofType:#"plist"];
}
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
tableData = [dict objectForKey:#"title"];
thumbnails = [dict objectForKey:#"Subtitle"];
cell.textLabel.text = [NSString stringWithFormat:#"%#",[thumbnails objectAtIndex:indexPath.row]];
return cell;
thumbnails is an Array. So When you retrieve the data from Array, Use [thumbnails objectAtIndex:indexPath.row] .

in UITableView first entry and last entry switching record data when loading from server

I am developing an app that will display data from server in a parallax type UITableView, and here is my code. Everything is loading great, but cell data(image, etc) keep switching from one cell to another.
- (void)viewDidLoad
{
[self hasInternet];
self.tableView.dataSource = self;
self.tableView.delegate = self;
[self loadData];
self.edgesForExtendedLayout=UIRectEdgeNone;
self.extendedLayoutIncludesOpaqueBars=NO;
self.automaticallyAdjustsScrollViewInsets=NO;
[super viewDidLoad];
}
- (void)viewWillAppear:(BOOL)animated
{
[self scrollViewDidScroll:nil];
[super viewWillAppear:animated];
[self loadData];
self.tableView.dataSource = self;
self.tableView.delegate = self;
}
- (void) loadData{
name = #"name";
email = #"email";
thumbnail = #"thumbnail";
myObject = [[NSMutableArray alloc] init];
NSData *jsonSource = [NSData dataWithContentsOfURL:[NSURL URLWithString:#"http://URL.php"]];
id jsonObjects = [NSJSONSerialization JSONObjectWithData:jsonSource options:NSJSONReadingMutableContainers error:nil];
for (NSDictionary *dataDict in jsonObjects) {
NSString *title_data = [dataDict objectForKey:#"fname"];
NSString *title_data2 = [dataDict objectForKey:#"lname"];
NSString *fulname = [NSString stringWithFormat:#"%# %#", title_data, title_data2];
NSString *emAil = [dataDict objectForKey:#"email"];
NSString *thumbnail_data = [dataDict objectForKey:#"img"];
thumbnail_data = [NSString stringWithFormat:#"http://URL/upload/%#",thumbnail_data];
dictionary = [NSDictionary dictionaryWithObjectsAndKeys: fulname, name, emAil, email, thumbnail_data, thumbnail, nil];
[myObject addObject:dictionary];
}
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return myObject.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"parallaxCell";
JBParallaxCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell=[[JBParallaxCell alloc]initWithStyle:
UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier];
}
NSDictionary *tmpDict = [myObject objectAtIndex:indexPath.row];
NSMutableString *text;
text = [NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:name]];
NSMutableString *mail;
mail = [NSMutableString stringWithFormat:#"%#",[tmpDict objectForKeyedSubscript:email]];
NSMutableString *images;
images = [NSMutableString stringWithFormat:#"%# ",[tmpDict objectForKey:thumbnail]];
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *url = [NSURL URLWithString:[tmpDict objectForKey:thumbnail]];
NSData *data = [NSData dataWithContentsOfURL:url];
dispatch_async(dispatch_get_main_queue(), ^{
cell.parallaxImage.image = [[UIImage alloc]initWithData:data];
});
});
cell.titleLabel.text = [NSString stringWithFormat:#"%#",text];
cell.subtitleLabel.text = [NSString stringWithFormat:#"%#",mail];
return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSArray *visibleCells = [self.tableView visibleCells];
for (JBParallaxCell *cell in visibleCells) {
[cell cellOnTableView:self.tableView didScrollOnView:self.view];
}
}
When I compile it, it shows all my data but then keep switching from one cell to another. Any help will be appreciated. Thanks
Because UITableView reuse the cell so when you scroll down or up the previous cell which is gone from your tableview bound is reuse. but it's image view has an image when it use last time so you have to clear first the imageview.
so put this line
cell.parallaxImage.image = nil;
Before the dispatch queue
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
You can use this code it will save the images in document directry and then show it.
please change the variable name.
NSString *imageURL = [[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"];
NSURL *url = [NSURL URLWithString:imageURL];
NSArray *seperate = [[[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"] componentsSeparatedByString:#"/"];
NSString *fileName = [NSString stringWithFormat:#"%#.png",[seperate objectAtIndex:seperate.count-1]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0]; // Get documents folder
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
NSError *error;
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath])
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
NSString *getImagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",fileName]];
UIImage *img = [UIImage imageWithContentsOfFile:getImagePath];
if ([img isKindOfClass:[UIImage class]]) {
//Set Downloaded Image
[cell.matchUserImageView setImage:img];
}
else {
if ([[ValidationString sharedManager] isNullString:[[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"]] == YES) {
//Set Default Image
[cell.matchUserImageView setImage:[UIImage imageNamed:#"photo_icon.png"]];
}
else{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
NSData *imageData = [NSData dataWithContentsOfURL:url];
NSArray *seperate = [[[matcheListArr objectAtIndex:indexPath.row] valueForKey:#"thumbnail_image"] componentsSeparatedByString:#"/"];
NSString *fileName = [NSString stringWithFormat:#"%#.png",[seperate objectAtIndex:seperate.count-1]];
dispatch_async(dispatch_get_main_queue(), ^{
// Update the UI
[cell.matchUserImageView setImage:[UIImage imageWithData:imageData]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:#"/MyFolder"];
NSString *savedImagePath = [dataPath stringByAppendingPathComponent:[NSString stringWithFormat:#"%#",fileName]];
[imageData writeToFile:savedImagePath atomically:NO];
});
});
}
}

Replaceing a string on a plist

I am making some confing settings using a tableView using a plist File to fill the data I am working around this code about 2 weeks and this appears to work.
This is my code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[week objectAtIndex:indexPath.row] setObject:#"1" forKey:#"checkmark"];
}
else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[[week objectAtIndex:indexPath.row] setObject:#"0" forKey:#"checkmark"];
}
[[week objectAtIndex:indexPath.row] writeToFile:#"week.plist" atomically:YES];
NSLog(#"%#, %#", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"]);
NSLog(#"Array de week seleccionando %#", week);
}
Everything seems to work fine on the debbug area (that's why I use the nslogs), and my problem is That can't save the changes on the plist file, it seem it saving in some kind of cache or something like that.
Update
I've also tried with this code:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *plistPath = [rootPath stringByAppendingPathComponent:#"week.plist"];
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects:week forKeys:week];
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict
format:NSPropertyListXMLFormat_v1_0
errorDescription:nil];
if ([[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
NSString *uno = #"1";
[[week objectAtIndex:indexPath.row] setObject:uno forKey:#"checkmark"];
}
else if ([[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"1"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
NSString *cero = #"0";
[plistData writeToFile:plistPath atomically:YES];
}
[[week objectAtIndex:indexPath.row] setObject:cero forKey:#"checkmarck"];
NSLog(#"%#, %#", selectedCell.textLabel.text,[[week objectAtIndex:indexPath.row] objectForKey:#"checkmark"]);
NSLog(#"Array de checkmarks %#", week);
}
And I have had the same result on the Debug Area:
2014-07-07 22:11:21.384 experiment table view[2264:60b] Thursday, 0
2014-07-07 22:11:21.385 experiment table view[2264:60b] Array de week seleccionando (
{
Name = Sunday;
checkmarck = 0;
},
{
Name = Monday;
checkmarck = 1;
},
{
Name = Tuesday;
checkmarck = 0;
},
{
Name = Wednesday;
checkmarck = 0;
},
{
Name = Thursday;
checkmarck = 0;
},
{
Name = Friday;
checkmarck = 1;
},
{
Name = Saturday;
checkmarck = 0;
}
)
Which is the same result that I get
If the Plist is in the bundle, you cannot write into it. 'In the bundle' means that you create the Plist by adding a new file in XCODE manually.
You can only write into a Plist that you create programmatically. Such a Plist is not in the bundle, but in your app's document directory. The path of the Plist is
NSArray *paths = NSSearchPathForDirectoriesInDomains
(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fileName = #"week.plist";
NSString *path = [NSString stringWithFormat:#"%#/%#", documentsDirectory, fileName];
Thanks to Sen I resolve this, his answer lead me to correct my code.
First I had to programmatically make the pList:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"week.plist"];
NSMutableArray *week = [[NSMutableArray alloc]init];
//Here I add all the objects inside the Mutable Array
[week writeToFile:path atomically:YES];
Then to write on the Plist by selecting it on the picker:
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:#"week.plist"];
NSMutableArray *array = [[NSMutableArray alloc]initWithContentsOfFile:path];
UITableViewCell *selectedCell = [tableView cellForRowAtIndexPath:indexPath];
if ([[[array objectAtIndex:indexPath.row] objectForKey:#"checkmark"] isEqual:#"0"]) {
[selectedCell setAccessoryType:UITableViewCellAccessoryCheckmark];
[[array objectAtIndex:indexPath.row] setObject:#"1" forKey:#"checkmark"];
} else {
[selectedCell setAccessoryType:UITableViewCellAccessoryNone];
[[array objectAtIndex:indexPath.row] setObject:#"0" forKey:#"checkmark"];
}
[array writeToFile:path atomically:YES];
[tableView reloadData];
[self.navigationController pushViewController:self animated:YES];
}
Thanks, and I hope this code help someone.

Get images from documents directory to collection view

How can I get images from documents directory to poplulate a collection view. So far I get all the images dumped into each cell according to my log, (or at least the image name is printed in the log)
First im getting the image names from the documents directory filelist is an NSMutable array of imageNames.png
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
fileList=[[NSMutableArray alloc]init];
for (NSString *filename in dirContents) {
NSString *fileExt = [filename pathExtension];
if ([fileExt isEqualToString:#"png"]) {
[fileList addObject:filename];
}
}
NSLog(#"document folder content list %# ",fileList);
This returns a list of my png file names in my NSMutsableArray fileList. Then I want to get all these images into my collection view
//set up cell from nib in viewDidLoad
UINib *cellNib = [UINib nibWithNibName:#"NibCell" bundle:nil];
[self.appliancesCollectionView registerNib:cellNib forCellWithReuseIdentifier:#"cvCell"];
/////
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
return fileList.count;
NSLog(#"collection view count is %#",fileList);
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
// Setup cell identifier
static NSString *cellIdentifier = #"cvCell";
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
NSString *fileName = [fileList objectAtIndex:indexPath.row];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageNamed: fileName]];
NSLog(#"cell Bg image %#",fileList);
return cell;
}
The probelm is nothing shows up in my collection view cells
The problem is that contentsOfDirectoryAtPath returns relative file paths. And you need absolute. Something like following should be used:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];
NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:documentsPath error:nil];
fileList=[[NSMutableArray alloc]init];
NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; // NEW LINE 1
for (NSString *filename in dirContents) {
NSString *fileExt = [filename pathExtension];
if ([fileExt isEqualToString:#"png"]) {
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:filename]; // NEW LINE 2
[fileList addObject:fullPath]; // NEW LINE 3
}
}
NSLog(#"document folder content list %# ",fileList);
You need to use imageWithContentsOfFile: instead imageNamed:
NSString * filePath = [[NSBundle mainBundle] pathForResource:<imageNameWithoutExtansion>ofType:<fileExtansion>];
cell.backgroundView = [[UIImageView alloc] initWithImage: [UIImage imageWithContentsOfFile: filePath]];

Cannot delete picture

I am saving my pictures into the collection view but I cannot delete the picture that I have took. I am using tap delete in the collection view. This is my code for that.However after I tap and delete the picture it looks like the picture is not deleted, and cannot find where is wrong, I am suspecting that it has something to do with the array but I am not sure.
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
allImagesArray = [[NSMutableArray alloc] init];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *locations = [[NSArray alloc]initWithObjects:#"Bottoms", #"Dress", #"Coats", #"Others", #"hats", #"Tops",nil ];
NSString *fPath = documentsDirectory;
NSMutableArray *trashCan = [NSMutableArray array];
NSArray *directoryContent;
for(NSString *component in locations){
NSString *TrashBin = [fPath stringByAppendingPathComponent:component];
NSArray *directoryContent = [[NSFileManager defaultManager] directoryContentsAtPath: TrashBin];
collectionTrash.delegate =self;
collectionTrash.dataSource=self;
for(NSString *str in directoryContent){
NSLog(#"str:%#", str);
NSString *finalFilePath = [TrashBin stringByAppendingPathComponent:str];
NSData *data = [NSData dataWithContentsOfFile:finalFilePath];
[trashCan addObject:finalFilePath];
if(data)
{
UIImage *image = [UIImage imageWithData:data];
[allImagesArray addObject:image];
NSLog(#"array:%#",[allImagesArray description]);
}}}
Trash = trashCan;
for(NSString *folder in locations) {
for(NSString *file in directoryContent) {
// load the image
}
}}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
NSLog(#"j");
return 1;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return [allImagesArray count];
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *reuseID = #"ReuseID";
TrashCell *mycell = (TrashCell *) [collectionView dequeueReusableCellWithReuseIdentifier:reuseID forIndexPath:indexPath];
UIImageView *imageInCell = (UIImageView*)[mycell viewWithTag:1];
imageInCell.image = [allImagesArray objectAtIndex:indexPath.row];
NSLog(#"a");
return mycell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
NSLog(#"s:%d", [Trash count]);
NSString *trashBin = [Trash objectAtIndex:indexPath.row];
NSLog(#"k%#l",trashBin);
[allImagesArray removeObjectAtIndex:indexPath.row];
[self.collectionTrash reloadData];
[self deleteMyFiles:trashBin];
}
NSString *myFileName;
-(void) deleteMyFiles:(NSString*)filePath {
NSError *error;
if([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
[[NSFileManager defaultManager] removeItemAtPath:filePath error:&error];
}
}
Be sure you delete the image in local memory also. When you delete image from collectionView not delete. Then stop the app and run it again see the deleted image is removed or not?. I think this is your problem. Then store all the image in single array and store it in plist. When you remove any image replace the array after removing the image in the array.
Use
[collectionView reloadData]
after calling deleteMyFiles.

Resources