问题
I am using map on my application, For pin drop, I want to set user's image instead of default pin.
I am downloading users image and setting it as per code I paste below.
For different scale of devices I am using image name as per device scale like eg.,
1 For non retina device - pin.png (size 30 x 30)
2 For retina devices - pin@2x.png (size 60 x 60)
3 For 6+ device - pin@3x.png (size 90 x 90)
Here for 1 and 2 working fine and image loads perfect But for 6+ (3x scale) it's not working
What's issue is:
For 6+ I am downloading pin@3x image, But on map it's size is 90 x 90 that should be 30 x 30. As it simply works for images when we use from application bundle.
For pin@2x.png it works fine and shows 2x image in size 30 x 30.
I also tried below solution by setting scale of image but not works
MKPinAnnotationView: Are there more than three colors available?
I have tried my best to explain actual issue, Can any one guide please if I am missing any thing or if it's require to set anything?
Code
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation
{
MKPinAnnotationView *annotationView = nil;
if ([annotation isKindOfClass:[MKUserLocation class]])
{
return nil;
}
if ([annotation isKindOfClass:[KPAnnotation class]])
{
//Custom annotation class for pin drop
KPAnnotation *a = (KPAnnotation *)annotation;
annotationView = (MKPinAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:@"pin"];
if (annotationView == nil)
{
annotationView = [[MKPinAnnotationView alloc] initWithAnnotation:[a.annotations anyObject]
reuseIdentifier:@"pin"];
}
//Image View to add subview in MKPinAnnotationView
UIImageView *imageView = [[UIImageView alloc] init];
UIImage * image = [UIImage imageNamed:@"pin.png"];
imageView.image=image;
//Test URL - see image name here
NSString *readAWSURL=@"<domainname.com>/pin@3x.png";
//Downloading image here to load with async way (SDWebImage)
[imageView sd_setImageWithURL:[NSURL URLWithString:readAWSURL] placeholderImage:[UIImage imageNamed:@"pin.png"]];
annotationView.image=imageView.image;
[annotationView addSubview:imageView];
annotationView.canShowCallout = YES;
}
return annotationView;
}
回答1:
Use MKAnnotationView in place of MKPinAnnotationView
- (MKAnnotationView ) mapView: (MKMapView ) mapViewIn viewForAnnotation:(id<MKAnnotation>) annotation
{
static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";
MKAnnotationView annotationView = (MKAnnotationView )[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
if (annotationView == nil)
{
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier];
}
if ([annotation isKindOfClass:[MapPoint class]]) {
annotationView.image = [UIImage imageNamed:@"orange.png"];
annotationView.canShowCallout = NO;
}
else{
annotationView.image = [UIImage imageNamed:@"blue.png"];
annotationView.canShowCallout = YES;
}
annotationView.annotation = annotation;
annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
return annotationView;
}
回答2:
My answer working nowadays for me. It based on pod 'SDWebImage'
,pod 'UIImage-Resize'
and bearded answer from @mokriya http://mokriya.tumblr.com/post/15342072745/dynamic-annotations-mkannotationview-for-ios
Just add category MKAnnotationView(WebCache):
#import <MapKit/MapKit.h>
@interface MKAnnotationView(WebCache)
- (void)setImageWithUrl:(NSURL *)url;
@end
and implementation (not a perfect but worked one):
#import "MKAnnotationView+WebCache.h"
#import "UIImageView+WebCache.h"
#import "UIImage+Resize.h"
#define kIconSize CGSizeMake(50, 50)
@implementation MKAnnotationView(WebCache)
- (void)setImageWithUrl:(NSURL *)url {
[[[UIImageView alloc] init] sd_setImageWithURL:url completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
self.image = [image resizedImageToSize:kIconSize];
}];
}
@end
and using:
#pragma mark - MKMapViewDelegate
- (nullable MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation {
static NSString *annotationViewReuseIdentifier = @"annotationViewReuseIdentifier";
MKAnnotationView *annotationView = (MKAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:annotationViewReuseIdentifier];
if (!annotationView) {
annotationView = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:annotationViewReuseIdentifier];
annotationView.canShowCallout = YES;
}
annotationView.annotation = <YOUR ANNOTATION>;
[annotationView setImageWithUrl:<HERE IS THE URL>];
return annotationView;
}
来源:https://stackoverflow.com/questions/30476231/mkpinannotationview-downloaded-image-3x-not-working-for-6