0

I have a star rating view which uses custom class RateView.m and .h files to show the star rating image and a label to show how much rating is given as given below:

enter image description here

Now when the rating is clicked, the selected image is shown and when unselected another image is shown.

This Rateview calls a delegate when that rateview is clicked

 - (void)rateView:(RateView *)rateView ratingDidChange:(float)rating {

        self.statusLabel.text = [NSString stringWithFormat:@"Rating: %0.1f/5.0", rating];
        NSString *valueToSave = self.statusLabel.text;
      //  self.rateView.fullSelectedImage = [UIImage imageNamed:@"yellow-rating.png"];
      //  UIImage *contactImage = self.rateView.fullSelectedImage;
        [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"ratingSelected"];
      //  [[NSUserDefaults standardUserDefaults] setObject:UIImagePNGRepresentation(contactImage) forKey:@"image"];
        [[NSUserDefaults standardUserDefaults] synchronize];

    }

In viewwillappear method it is as shown:

 -(void) viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];

        NSString *savedValue = [[NSUserDefaults standardUserDefaults] stringForKey:@"ratingSelected"];
        self.statusLabel.text = savedValue;
        NSData *imageData = [[NSUserDefaults standardUserDefaults] objectForKey:@"image"];
       // NSLog(@"imagedata is %@",imageData);
        UIImage *contactImage = [UIImage imageWithData:imageData];
         NSLog(@"contactImage is %@",contactImage);
       // self.rateView.fullSelectedImage = contactImage;
         self.rateView.fullSelectedImage = [UIImage imageNamed:@"yellow-rating.png"];
    }

Now the issue is i can see the label with correct rating,but the image is not saved using userdefault.
How to also save the image using userdefault in my case.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Saranya
  • 595
  • 2
  • 7
  • 19
  • Your `rateView: ratingDidChange:` method currently only sets the statusLabel. How do you set the image you want to save? – LGP Jan 20 '18 at 13:15
  • in viewdid load method self.rateView.notSelectedImage = [UIImage imageNamed:@"grey-rating.png"]; // self.rateView.halfSelectedImage = [UIImage imageNamed:@"kermit_half.png"]; self.rateView.fullSelectedImage = [UIImage imageNamed:@"yellow-rating.png"]; self.rateView.rating = 0; self.rateView.editable = YES; self.rateView.maxRating = 5; self.rateView.delegate = self; – Saranya Jan 23 '18 at 10:29
  • This is how initially i set the image when it is selected or not – Saranya Jan 23 '18 at 10:30
  • Wouldn't it be better to save `self.rateView.rating`? The images are available in your app already. – LGP Jan 23 '18 at 13:01
  • This again saves the float value which is the number,but i want to save those images too like say if someone rated 3.0,then 3 stars image will change,so that also i want to save somewhere. – Saranya Jan 23 '18 at 14:05

1 Answers1

0

You can save image to disc (for example Document directory) and store only image's path in NSUserDefaults

See this answer

Alexey Kudlay
  • 525
  • 2
  • 14
  • yes i am able to save the image to document directory, but i want to save the entire view i.e if three stars are clicked then i want to show three stars image even if moved to different view controller and i come back. – Saranya Jan 16 '18 at 16:43