Upload
michael-pan
View
319
Download
0
Embed Size (px)
Citation preview
NoteSomethingMichael Pan
13年9月2⽇日星期⼀一
Looks like
13年9月2⽇日星期⼀一
Additional frameworks used• CoreData• CoreLocation• MapKit
13年9月2⽇日星期⼀一
Core Data
13年9月2⽇日星期⼀一
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⽇日星期⼀一
New project
13年9月2⽇日星期⼀一
Check Core Data
13年9月2⽇日星期⼀一
Storyboard
13年9月2⽇日星期⼀一
Classes
13年9月2⽇日星期⼀一
Collect all data
13年9月2⽇日星期⼀一
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⽇日星期⼀一
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⽇日星期⼀一
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⽇日星期⼀一
Show info
13年9月2⽇日星期⼀一
InfoViewController
#import <CoreLocation/CoreLocation.h>@interface InfoViewController : UIViewController@property NSString * notes;@property UIImage * image;@property CLLocationCoordinate2D position;@end
13年9月2⽇日星期⼀一
InfoViewController.m- (void)viewDidLoad{ [super viewDidLoad]; self.imageView.image = self.image; self.noteLabel.text = self.notes; self.mapView.delegate = self; MyAnnotation * annotation = [MyAnnotation new]; annotation.coordinate = self.position; [self.mapView addAnnotation:annotation];}
13年9月2⽇日星期⼀一
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⽇日星期⼀一
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⽇日星期⼀一
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⽇日星期⼀一
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⽇日星期⼀一
Question
13年9月2⽇日星期⼀一