目前,試圖通過URL下載圖像,並以Apple Store商店完成PhotoScroller應用程序的方式平鋪這些圖像。 最大的圖像尺寸是4000 * 4000,並且是RGB格式。我用tilesize 256 * 256平鋪這些圖像。 需要很長時間才能平鋪圖像並將文件寫入磁盤。 正在尋找更好的方法,如果它可能平鋪文件和加載在iphone上。共有4個URL調用和每個URL調用獲取圖像,並通過平鋪過程並調用PhotoScroller應用程序在iphone上啓動這些圖像。有沒有更好的方式平鋪iPhone中的圖像
如果有人遇到過這樣的問題並且對它有更多的瞭解,你能否分享一些想法。
-(void) startImageDownloading
{
if (!isImageRequested)
{
isImageRequested = YES;
self.responseData = [NSMutableData data];
URL1 = @"http://www.abc.com/image_id=1&size=1";
NSURLRequest *request1 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL1]];
self.connection1= [[[NSURLConnection alloc] initWithRequest:request1 delegate:self] autorelease];
URL2 = @"http://www.abc.com/image_id=2&size=2";
NSURLRequest *request2 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL2]];
self.connection2= [[[NSURLConnection alloc] initWithRequest:request2 delegate:self] autorelease];
URL3 = @"http://www.abc.com/image_id=3&size=3";
NSURLRequest *request3 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL3]];
self.connection3= [[[NSURLConnection alloc] initWithRequest:request3 delegate:self] autorelease];
URL4 = @"http://www.abc.com/image_id=4&size=4";
NSURLRequest *request4 = [NSURLRequest requestWithURL:[NSURL URLWithString:URL4]];
self.connection4= [[[NSURLConnection alloc] initWithRequest:request4 delegate:self] autorelease];
}
}
- (void)saveTilesOfSize:(CGSize)size
forImage:(UIImage*)image
toDirectory:(NSString*)directoryPath
usingPrefix:(NSString*)prefix
{
CGFloat cols = [image size].width/size.width;
CGFloat rows = [image size].height/size.height;
int fullColumns = floorf(cols);
int fullRows = floorf(rows);
CGFloat remainderWidth = [image size].width - (fullColumns * size.width);
CGFloat remainderHeight = [image size].height - (fullRows * size.height);
if (cols > fullColumns) fullColumns++;
if (rows > fullRows) fullRows++;
CGImageRef fullImage = [image CGImage];
for (int y = 0; y < fullRows; ++y) {
for (int x = 0; x < fullColumns; ++x) {
CGSize tileSize = size;
if (x + 1 == fullColumns && remainderWidth > 0) {
// Last column
tileSize.width = remainderWidth;
}
if (y + 1 == fullRows && remainderHeight > 0) {
// Last row
tileSize.height = remainderHeight;
}
CGImageRef tileImage = CGImageCreateWithImageInRect(fullImage,(CGRect){{x*size.width, y*size.height},tileSize});
NSData *imageData = UIImagePNGRepresentation([UIImage imageWithCGImage:tileImage]);
CGImageRelease(tileImage);
NSString *path = [NSString stringWithFormat:@"%@/%@%d_%d.png",directoryPath, prefix, x, y];
[imageData writeToFile:path atomically:NO];
}
}
}
+1 ...不錯的回答方式 –
謝謝曼尼。我想如果我可以將文件保存到磁盤,並行將幫助我解決問題。但不知道你是如何平行進行的?你的意思是使用NSOperationQueue嗎? – lifemoveson
我想像這樣:4個異步連接並行檢索並保存到磁盤,然後在主線程上執行渲染。對不起併發的混淆,我的意思是超過1個線程將同時從服務器檢索和處理數據。 – Manny