More Related Content
More from Michael Pan (15)
Note something
- 5. Create Class for NSManagedObject
@interface Record : NSManagedObject
@property (nonatomic, retain) NSDate * timeStamp;
@property (nonatomic, retain) NSString * notes;
@property (nonatomic, retain) NSNumber * latitude;
@property (nonatomic, retain) NSNumber * longitude;
@property (nonatomic, retain) NSString * imagePath;
@end
13年9月2⽇日星期⼀一
- 11. CoreLocation - DetailViewController
- (void)configureView
{
locationManager= [CLLocationManager new];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
-(void) locationManager:(CLLocationManager
*)manager didUpdateLocations:(NSArray *)locations{
self.currentLocation = [ (CLLocation *)
[locations lastObject] coordinate];
}
13年9月2⽇日星期⼀一
- 12. Get data
- (IBAction)recordNote:(id)sender {
NSString * notes = self.noteField.text;
CGFloat latitude = self.currentLocation.latitude;
CGFloat longitude = self.currentLocation.longitude;
UIImage * image = self.imageView.image;
NSDate * date = [NSDate date];
NSString * fileName = [NSString stringWithFormat:@"%@",date];
NSString * imagePath = [[self docPath]
stringByAppendingPathComponent:fileName];
if (image == nil) {
NSLog(@"No image...");
return;
}
// save to data base
}
13年9月2⽇日星期⼀一
- 13. Save data
if ([self saveImage:UIImagePNGRepresentation(image) withPath:imagePath]) {
Record * record = [NSEntityDescription
insertNewObjectForEntityForName:@"Record"
inManagedObjectContext:self.manangedContext];
record.notes = notes;
record.latitude = @(latitude);
record.longitude = @(longitude);
record.imagePath = fileName;
record.timeStamp = date;
[self.manangedContext save:NULL];
}
-(BOOL) saveImage:(NSData *) imageData withPath:(NSString *) filePath{
return [[NSFileManager defaultManager] createFileAtPath:filePath
contents:imageData attributes:nil];
}
13年9月2⽇日星期⼀一
- 17. MKAnnotationView
-(MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:
(id<MKAnnotation>)annotation{
MKAnnotationView * annotationView = [mapView
dequeueReusableAnnotationViewWithIdentifier:@"POI"];
if (annotationView == nil) {
annotationView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:@"POI"];
MKPinAnnotationView * pinView = annotationView;
pinView.pinColor = MKPinAnnotationColorPurple;
}
return annotationView;
}
13年9月2⽇日星期⼀一
- 18. Move Region
-(void) viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
CLLocationCoordinate2D coordination = self.position;
MKCoordinateSpan mySpan = {0.05f, 0.05f};
MKCoordinateRegion toRegion = {coordination, mySpan};
[self.mapView setRegion:toRegion animated:YES];
}
13年9月2⽇日星期⼀一
- 19. MasterViewController
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath
{
NSManagedObject *object = [self.fetchedResultsController
objectAtIndexPath:indexPath];
cell.textLabel.text = [[object valueForKey:@"notes"] description];
cell.detailTextLabel.text = [[object valueForKey:@"timeStamp"] description];
NSString * imagePath = [object valueForKey:@"imagePath"];
UIImage * image = [UIImage imageWithContentsOfFile:[[self docPath]
stringByAppendingPathComponent:imagePath]];
cell.imageView.image = image;
}
13年9月2⽇日星期⼀一
- 20. MasterViewController.m
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.identifier isEqualToString:@"record"]) {
DetailViewController * detailViewController =
segue.destinationViewController;
detailViewController.manangedContext = self.managedObjectContext;
}
if ([segue.identifier isEqualToString:@"showInfo"]) {
NSIndexPath * indexPath = [self.tableView indexPathForSelectedRow];
Record * record = [self.fetchedResultsController
objectAtIndexPath:indexPath];
InfoViewController * info = segue.destinationViewController;
info.notes = record.notes;
info.position = CLLocationCoordinate2DMake([record.latitude doubleValue],
[record.longitude doubleValue]);
info.image = [UIImage imageWithContentsOfFile:[[self docPath]
stringByAppendingPathComponent:record.imagePath]];
}
}
13年9月2⽇日星期⼀一