들어 가지 :iOS MKMapView를 UIImage로 변환 중입니다. 절대로이 답변에 따라 블록
내가 사진 내지도를 변환하는 시도를하지만, 앱은 결코 snapshotter 블록을 입력합니다.
왜?
//Get location an then get a Picture of the Map.
CLLocation *userLoc = self.map.userLocation.location; //self.map is a MKMapView;
CLLocationCoordinate2D punto = userLoc.coordinate;
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(punto, 500, 500);
[self.map setRegion:(region)];
[self.map setShowsUserLocation:YES];
//Place a Pin in actual location.
MKPointAnnotation *pin = [[MKPointAnnotation alloc]init];
pin.coordinate = punto;
pin.title = @"Localización";
[self.map addAnnotation:pin];
//Convert map to picture.
MKMapSnapshotOptions *options = [[MKMapSnapshotOptions alloc] init];
options.region = self.map.region;
options.scale = [UIScreen mainScreen].scale;
options.size = self.map.frame.size;
MKMapSnapshotter *snapshotter = [[MKMapSnapshotter alloc] initWithOptions:options];
[snapshotter startWithQueue:dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) completionHandler:^(MKMapSnapshot *snapshot, NSError *error) {
NSLog(@"Entering the block."); //Never prints!
// get the image associated with the snapshot
UIImage *image = snapshot.image;
NSLog(@"imagen %@",image); //Niether do this!
// Get the size of the final image
CGRect finalImageRect = CGRectMake(0, 0, image.size.width, image.size.height);
// Get a standard annotation view pin. Clearly, Apple assumes that we'll only want to draw standard annotation pins!
MKAnnotationView *pin = [[MKPinAnnotationView alloc] initWithAnnotation:nil reuseIdentifier:@""];
UIImage *pinImage = pin.image;
// ok, let's start to create our final image
UIGraphicsBeginImageContextWithOptions(image.size, YES, image.scale);
// first, draw the image from the snapshotter
[image drawAtPoint:CGPointMake(0, 0)];
// now, let's iterate through the annotations and draw them, too
for (id<MKAnnotation>annotation in self.map.annotations)
{
CGPoint point = [snapshot pointForCoordinate:annotation.coordinate];
if (CGRectContainsPoint(finalImageRect, point)) // this is too conservative, but you get the idea
{
CGPoint pinCenterOffset = pin.centerOffset;
point.x -= pin.bounds.size.width/2.0;
point.y -= pin.bounds.size.height/2.0;
point.x += pinCenterOffset.x;
point.y += pinCenterOffset.y;
[pinImage drawAtPoint:point];
}
}
// grab the final image
UIImage *finalImage = UIGraphicsGetImageFromCurrentImageContext();
NSLog(@"Picture inside the block %@",finalImage); //Never prints.
UIGraphicsEndImageContext();
// and save it
NSData *data = UIImagePNGRepresentation(finalImage);
[data writeToFile:@"Picture.jpg" atomically:YES];
if (error) {
NSLog(@"Error"); //This is not printed.
}else{
NSLog(@"Success!"); //Neither do this.
self.fotoParaEnviar = finalImage;
}
}];
NSLog(@"Picture outside the block %@",self.fotoParaEnviar); //This is allway NULL
모든 것이 잘 인스턴스화 된 것처럼 보입니다.
그럼 왜 블록이 실행되지 않습니까?
이 문제는 재현 할 수 없지만 본 사람은 본인이 아닙니다. http://stackoverflow.com/questions/22774090/ios-mkmapshapshotter-completion-block-is-not-always-being- – Rob