我正在尝试从服务器提取图像并使用CATiledLayer显示它们。 然而,图像似乎已损坏(它们有一些随机的空白点),我的猜测是因为多个连接可能已将其数据保存到同一个变量。所以我尝试使用NSMutableDictionary来跟踪特定NSURLConnection将NSMutableData保存到哪个NSMutableData实例。无论如何,事情只会变得更糟,因为现在只出现了空白的瓷砖。
h。档案:
#import <UIKit/UIKit.h>
@interface KlotsideVaade : UIView <NSURLConnectionDelegate> {
NSMutableData *andmedServerist;
NSMutableDictionary*andmedServeristDictionary;
UIImage*minuPilt;
NSString *imageName;
BOOL annotates;
int Ymin;
int Ymax;
int Xmin;
int Xmax;
}
@property(nonatomic) NSMutableData *andmedServerist;
@property(nonatomic) NSMutableDictionary*andmedServeristDictionary;
@property(nonatomic) UIImage*minuPilt;
@property (assign) BOOL annotates;
- (void)looYhendus;
- (id)initWithImage;
- (UIImage *)tileForScale;
@end
m。文件:
#import "KlotsideVaade.h"
#import <QuartzCore/CATiledLayer.h>
@implementation KlotsideVaade
@synthesize andmedServerist;
@synthesize andmedServeristDictionary;
@synthesize minuPilt;
@synthesize annotates;
+ (Class)layerClass {
return [CATiledLayer class];
}
- (id)initWithImage
{
if ((self = [super initWithFrame:CGRectMake(0, 0, 4096, 2992)])) {
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
tiledLayer.levelsOfDetail = 4;
}
return self;
}
- (void)drawRect:(CGRect)rect {
CGContextRef context = UIGraphicsGetCurrentContext();
CGFloat scale = CGContextGetCTM(context).a;
CGSize tileSize = CGSizeMake(512, 374);
tileSize.width /= scale;
tileSize.height /= scale;
int firstCol = floorf(CGRectGetMinX(rect) / tileSize.width);
int lastCol = floorf((CGRectGetMaxX(rect)-1) / tileSize.width);
int firstRow = floorf(CGRectGetMinY(rect) / tileSize.height);
int lastRow = floorf((CGRectGetMaxY(rect)-1) / tileSize.height);
for (int row = firstRow; row <= lastRow; row++) {
for (int col = firstCol; col <= lastCol; col++) {
Ymin=585000+col*10000/(4*scale);
Ymax=585000+(col+1)*10000/(4*scale);
Xmin=6500000-(row+1)*15000/(4*scale);
Xmax=6500000-row*15000/(4*scale);
UIImage *tile = [self tileForScale];
CGRect tileRect = CGRectMake(tileSize.width * col, tileSize.height * row, tileSize.width, tileSize.height);
tileRect = CGRectIntersection(self.bounds, tileRect);
[tile drawInRect:tileRect];
if (self.annotates) {
[[UIColor whiteColor] set];
CGContextSetLineWidth(context, 6.0 / scale);
CGContextStrokeRect(context, tileRect);
}
}
}
}
- (UIImage *)tileForScale;
{
[self looYhendus];
UIImage *image=minuPilt;
return image;
}
- (void)looYhendus
{
NSString *aadress = [NSString stringWithFormat:@"http://xgis.maaamet.ee/wms-pub/alus?version=1.1.1&service=WMS&request=GetMap&layers=MA-ALUSKAART&styles=default&srs=EPSG:3301&BBOX=%d,%d,%d,%d&width=%d&height=%d&format=image/png",Ymin,Xmin,Ymax,Xmax,512,374];
NSURL *url = [[NSURL alloc] initWithString:aadress];
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url];
NSURLConnection *theConnection = [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if( theConnection )
{
andmedServerist = [NSMutableData data];
[andmedServeristDictionary setObject:andmedServerist forKey:[theConnection description]];
}
CFRunLoopRun();
}
-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
andmedServerist=[andmedServeristDictionary objectForKey:[connection description]];
[andmedServerist setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
andmedServerist=[andmedServeristDictionary objectForKey:[connection description]];
[andmedServerist appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"No connection" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil];
CFRunLoopStop(CFRunLoopGetCurrent());
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
andmedServerist=[andmedServeristDictionary objectForKey:[connection description]];
minuPilt = [UIImage imageWithData: andmedServerist];
CFRunLoopStop(CFRunLoopGetCurrent());
}
@end