21
NoteSomething Michael Pan 1392星期

Note something

Embed Size (px)

Citation preview

Page 1: Note something

NoteSomethingMichael Pan

13年9月2⽇日星期⼀一

Page 2: Note something

Looks like

13年9月2⽇日星期⼀一

Page 3: Note something

Additional frameworks used• CoreData• CoreLocation• MapKit

13年9月2⽇日星期⼀一

Page 4: Note something

Core Data

13年9月2⽇日星期⼀一

Page 5: Note something

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⽇日星期⼀一

Page 6: Note something

New project

13年9月2⽇日星期⼀一

Page 7: Note something

Check Core Data

13年9月2⽇日星期⼀一

Page 8: Note something

Storyboard

13年9月2⽇日星期⼀一

Page 9: Note something

Classes

13年9月2⽇日星期⼀一

Page 10: Note something

Collect all data

13年9月2⽇日星期⼀一

Page 11: Note something

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⽇日星期⼀一

Page 12: Note something

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⽇日星期⼀一

Page 13: Note something

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⽇日星期⼀一

Page 14: Note something

Show info

13年9月2⽇日星期⼀一

Page 15: Note something

InfoViewController

#import <CoreLocation/CoreLocation.h>@interface InfoViewController : UIViewController@property NSString * notes;@property UIImage * image;@property CLLocationCoordinate2D position;@end

13年9月2⽇日星期⼀一

Page 16: Note something

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⽇日星期⼀一

Page 17: Note something

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⽇日星期⼀一

Page 18: Note something

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⽇日星期⼀一

Page 19: Note something

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⽇日星期⼀一

Page 20: Note something

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⽇日星期⼀一

Page 21: Note something

Question

13年9月2⽇日星期⼀一