SlideShare a Scribd company logo
1 of 21
Download to read offline
NoteSomething
Michael 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⽇日星期⼀一

More Related Content

More from Michael Pan

Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android StudioMichael Pan
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and GenymotionMichael Pan
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migrationMichael Pan
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Michael Pan
 
Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cardsMichael Pan
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdfMichael Pan
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 StevenMichael Pan
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperMichael Pan
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. JackyMichael Pan
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017Michael Pan
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Michael Pan
 

More from Michael Pan (15)

Activity
ActivityActivity
Activity
 
Introduction to Android Studio
Introduction to Android StudioIntroduction to Android Studio
Introduction to Android Studio
 
Eclipse and Genymotion
Eclipse and GenymotionEclipse and Genymotion
Eclipse and Genymotion
 
Core data lightweight_migration
Core data lightweight_migrationCore data lightweight_migration
Core data lightweight_migration
 
Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4Google maps SDK for iOS 1.4
Google maps SDK for iOS 1.4
 
Homework2 play cards
Homework2 play cardsHomework2 play cards
Homework2 play cards
 
Dropbox sync
Dropbox syncDropbox sync
Dropbox sync
 
Nimbus
NimbusNimbus
Nimbus
 
Superstar dj pdf
Superstar dj pdfSuperstar dj pdf
Superstar dj pdf
 
ADB - Arthur
ADB - ArthurADB - Arthur
ADB - Arthur
 
比價撿便宜 Steven
比價撿便宜 Steven比價撿便宜 Steven
比價撿便宜 Steven
 
Appsgaga - iOS Game Developer
Appsgaga - iOS Game DeveloperAppsgaga - iOS Game Developer
Appsgaga - iOS Game Developer
 
GZFox Inc. Jacky
GZFox Inc. JackyGZFox Inc. Jacky
GZFox Inc. Jacky
 
創投公司 hoku_20121017
創投公司 hoku_20121017創投公司 hoku_20121017
創投公司 hoku_20121017
 
Opening iOS App 開發者交流會
Opening iOS App 開發者交流會Opening iOS App 開發者交流會
Opening iOS App 開發者交流會
 

Note something

  • 3. Additional frameworks used • CoreData • CoreLocation • MapKit 13年9月2⽇日星期⼀一
  • 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⽇日星期⼀一
  • 15. InfoViewController #import <CoreLocation/CoreLocation.h> @interface InfoViewController : UIViewController @property NSString * notes; @property UIImage * image; @property CLLocationCoordinate2D position; @end 13年9月2⽇日星期⼀一
  • 16. 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⽇日星期⼀一
  • 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⽇日星期⼀一