In a DetailView I am trying to show more than 1 image from Parse.com backend so I have this code:
EDIT:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
VestimentaDetailCell *cell = (VestimentaDetailCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
static int index = 1;
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%d", index ]];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageFile.image = [UIImage imageWithData:data];
}
}];
return cell;
}
It works.. The problem is that once the user has viewed the images once they no longer appear again.
So I tried just implementing:
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%d"]];
But then I get the error of "More '%' conversions than data arguments.
How do I set the code?
Change
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%d"]];
To
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%#", index ]];
When u write something like NSLog(#" %d %f %#"), u should pass arguments like this :
NSLog(#"%d %f %#", someInt, someFloat, someString);
You can try this:
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%d+1", indexPath.row ]];
Hope that help.
You need to provide an argument for the %d in the string format.
Perhaps you need this:
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%d", index]];
With the help of all the answers in this questions I figured it out.
Here is the code for anyone who encounters the same problem:
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = #"Cell";
VestimentaDetailCell *cell = (VestimentaDetailCell *) [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
PFFile *imageFile = [self.vestimenta objectForKey:[NSString stringWithFormat:#"image_%d", indexPath.row]];
[imageFile getDataInBackgroundWithBlock:^(NSData *data, NSError *error) {
if (!error) {
cell.imageFile.image = [UIImage imageWithData:data];
}
}];
return cell;
}
Related
I have a smooth scrolling problem with the UITableView. Can anyone help me where is the problem? The code I have written is this-
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
LabTestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}//cell.txtHeading.text=#"hello";
cell.txtHeading.text=[[_ResponseDic valueForKey:#"title"]objectAtIndex:indexPath.row];
cell.txtdescribtion.text=[[_ResponseDic valueForKey:#"description"]objectAtIndex:indexPath.row];
cell.txtPrice.text=[[_ResponseDic valueForKey:#"purchase_price"]objectAtIndex:indexPath.row];
cell.txtLabName.text=[[_ResponseDic valueForKey:#"brand"]objectAtIndex:indexPath.row];
NSString *path=#"https://www.fingerfry.com/pharmgo/uploads/product_image/";
NSString *url=[path stringByAppendingString:[[_ResponseDic valueForKey:#"main_image"]objectAtIndex:indexPath.row]];
NSData* imageData = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:url]];
UIImage* image = [[UIImage alloc] initWithData:imageData];
cell.image.image=image;
return cell;
}
If you load the image in cellForRowAtIndexPath, use Asynchronous method backgroundly load the images, use this below code,
dispatch_queue_t image_queue = dispatch_queue_create("com.company.app.imageQueue", NULL);
dispatch_queue_t main_queue = dispatch_get_main_queue();
NSString *path=#"https://www.fingerfry.com/pharmgo/uploads/product_image/";
NSString *url=[path stringByAppendingString:[[_ResponseDic valueForKey:#"main_image"]objectAtIndex:indexPath.row]];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
NSString *cachePath = [paths objectAtIndex:0];
NSString *dataPath = [cachePath stringByAppendingPathComponent:url];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( [fileManager fileExistsAtPath:dataPath] )
{
UIImage *image = [UIImage imageWithContentsOfFile:dataPath];
[cell.imgView setImage:image];
}
else
{
[cell.imgView setImage:[UIImage imageNamed:#"images.png"]];
dispatch_async(image_queue, ^{
NSData *imageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:url]];
UIImage *image = [UIImage imageWithData:imageData];
[UIImagePNGRepresentation(image) writeToFile:dataPath atomically:YES];
dispatch_async(main_queue, ^{
[cell.imgView setImage:image];
});
});
}
I have the same issue, i had put the above code in cellForRowAtIndexPath, its working normal scrolling, hope its helpful
First install the pod:
pod 'SDWebImage/WebP', '~>3.7' in pod file.
Then you have to import:
#import <SDWebImage/UIImageView+WebCache.h>
Write below code:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = #"CustomTableCell";
LabTestCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if(cell == nil)
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:#"CustomTableCell" owner:self options:nil];
cell = [nib objectAtIndex:0];
}//cell.txtHeading.text=#"hello";
cell.txtHeading.text=[[_ResponseDic valueForKey:#"title"]objectAtIndex:indexPath.row];
cell.txtdescribtion.text=[[_ResponseDic valueForKey:#"description"]objectAtIndex:indexPath.row];
cell.txtPrice.text=[[_ResponseDic valueForKey:#"purchase_price"]objectAtIndex:indexPath.row];
cell.txtLabName.text=[[_ResponseDic valueForKey:#"brand"]objectAtIndex:indexPath.row];
[cell.image sd_setImageWithURL:[NSURL URLWithString:[obj valueForKey:IMAGE_KEY]] placeholderImage:nil options:SDWebImageRefreshCached completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
}];
return cell;
}
Reference link :
https://github.com/rs/SDWebImage
I am using the UITableView.
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLink];
This is the line I am getting the error. It is working in IOS 7. But when I run the application in IOS 8 I am getting the error
** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Must translate autoresizing mask into constraints to have _setHostsLayoutEngine:YES.
EDIT
Full code
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
static NSString *CellIdentifierImageLink = #"NewsImageAndLink";
static NSString *CellIdentifierImage = #"NewsImage";
static NSString *CellIdentifierLink = #"NewsLink";
static NSString *CellIdentifier = #"NewsDescription";
NSString *image=[[_news objectAtIndex:indexPath.row] valueForKey:#"imageURL"];
NSString *link=[[_news objectAtIndex:indexPath.row] valueForKey:#"link"];
NSString *description=[[_news objectAtIndex:indexPath.row] valueForKey:#"description"];
NSString *date=[[_news objectAtIndex:indexPath.row] valueForKey:#"date"];
NSString *title=[[_news objectAtIndex:indexPath.row] valueForKey:#"title"];
NSMutableString *html = [NSMutableString stringWithString: #""];
//continue building the string
[html appendString:#"<html><body>"];
[html appendString:description];
[html appendString:#"</body></html>"];
SDWebImageManager *manager = [SDWebImageManager sharedManager];
if (image !=(NSString *)[NSNull null] && link !=(NSString *)[NSNull null]) {
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierImageLink];
cell.lblHeading.text=title;
NSURL *url = [NSURL URLWithString:image];
[manager downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
cell.newsImage.image = image;
}];
if (date !=(NSString *)[NSNull null] ) {
cell.lblDate.text=date;
}
//pass the string to the webview
[cell.webView loadHTMLString:[html description] baseURL:nil];
cell.lblLink.text=link;
return cell;
}
else if (image !=(NSString *)[NSNull null] && link==(NSString *)[NSNull null]) {
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierImage];
cell.lblHeading.text=title;
NSURL *url = [NSURL URLWithString:image];
[manager downloadImageWithURL:url options:0 progress:^(NSInteger receivedSize, NSInteger expectedSize) {
} completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
cell.newsImage.image = image;
}];
if (date !=(NSString *)[NSNull null] ) {
cell.lblDate.text=date;
}
[cell.webView loadHTMLString:[html description] baseURL:nil];
return cell;
}
else if (image ==(NSString *)[NSNull null] && link!=(NSString *)[NSNull null]) {
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifierLink];
cell.lblHeading.text=title;
if (date !=(NSString *)[NSNull null] ) {
cell.lblDate.text=date;
}
//cell.txtDescription.text=description;
//pass the string to the webview
[cell.webView loadHTMLString:[html description] baseURL:nil];
cell.lblLink.text=link;
return cell;
}
else if (image ==(NSString *)[NSNull null] && link==(NSString *)[NSNull null]) {
CategoryCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
cell.lblHeading.text=title;
if (date !=(NSString *)[NSNull null] ) {
cell.lblDate.text=date;
}
//cell.txtDescription.text=description;
//pass the string to the webview
[cell.webView loadHTMLString:[html description] baseURL:nil];
return cell;
}
return nil;
}
Check out the answer here: why do i get "Must translate autoresizing mask into constraints to have _setHostsLayoutEngine:YES" in xcode 6 beta
This solved it for me temporarily.
So i have problem...For me, Having the dispatch_async code within cellForRowAtIndexPath method is too clunky and messy for me.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = #"myCellID";
myCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
// someData - an array of dictionary for the tableview datasource
NSURL * imageURL = [NSURL URLWithString:someData[indexPath.row][#"photoURL"]];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *image = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
cell.myImageView.image = image;
});
});
}
So after a little research my solution to this was:
-(void)loadAsyncImage:(NSIndexPath *)indexPath{
NSURL * imageURL = [NSURL URLWithString:someData[indexPath.row][#"photoURL"] ];
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0ul);
dispatch_async(queue, ^{
NSData *data = [NSData dataWithContentsOfURL:imageURL];
UIImage *myImage = [UIImage imageWithData:data];
dispatch_async(dispatch_get_main_queue(), ^{
// _myTableView a property link from the interface builder
myCell * cell = (id)[_myTableView cellForRowAtIndexPath:indexPath];
cell.myImageView.image = myImage;
});
});
}
and then in
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath {
static NSString *CellIdentifier = #"myCellID";
myCell * cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath];
[self loadAsyncImage: indexPath];
}
To my surprise it works and is so much more cleaner...but... i dont know if this is the right way to do this. Is there a better approach to this issue? or is this ok?
Hi I am trying the code bellow and I can't get my image showing
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath object:(PFObject *)object
{
static NSString *CellIdentifier = #"Cell";
PFTableViewCell *cell = (PFTableViewCell *)[tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[PFTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
}
// Configure the cell
cell.textLabel.text = [object objectForKey:#"venueName"];
cell.imageView.file = [object objectForKey:#"image"];
[cell.imageView.file getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
cell.imageView.image = [UIImage imageWithData:data];
}];
return cell;
}
I also try:
[cell.imageView loadInBackground];
and nothing. Do I need to insert manually an UIImage in the cell?
Regards
To load a image from Parse.com (using PFImageView), you just do it:
creature.file = (PFFile *)file;
[creature loadInBackground];
Are you sure that the property "image" is a file? If it is, try this:
PFFile *theImage = [object objectForKey:#"image"];
[theImage getDataInBackgroundWithBlock:^(NSData *data, NSError *error){
NSData *imageFile = [theImage getData];
cell.imageView.image = [UIImage imageWithData:imageFile];
}];
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.