2012-03-30 79 views
0

哪一個在運行時更好: 1)在sqlite中保存大小爲250X120的圖像。 2)在文檔目錄中保存相同大小的圖像。 在兩個地方,應用程序需要將這些保存的圖像顯示在控件中。 另外我有要求顯示最多20個圖像。在iphone應用程序運行時保存圖像

回答

1

第二個選項比第一個好得多。爲了實現第二部分使用:

- (void)saveImage:(UIImage*)image:(NSString*)imageName 
{ 
    NSData *imageData = UIImagePNGRepresentation(image); //convert image into .png format. 

    NSFileManager *fileManager = [NSFileManager defaultManager];//create instance of NSFileManager 

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //create an array and store result of our search for the documents directory in it 

    NSString *documentsDirectory = [paths objectAtIndex:0]; //create NSString object, that holds our exact path to the documents directory 

    NSString *fullPath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.png", imageName]]; //add our image to the path 

    [fileManager createFileAtPath:fullPath contents:imageData attributes:nil]; //finally save the path (image) 

    NSLog(@"image saved"); 
} 
+0

謝謝...我知道實現.... :) – adi27 2012-03-30 14:25:00

1

+1 M.Sharjeel - 我們用什麼通常是一個半混合,在那裏我們將有一個核心數據對象(由sqlite的在手機/ PAD支持),這具有關於快速搜索的文件的元數據,然後將NSString存儲到documentsDirectory中的路徑。

+0

同意他的看法..核心數據是有效的,並且執行比SQLite的更好 – 2012-03-30 12:22:45

0

如果你採取文件目錄的方式,這是一個很好的方法來實現它。

-(void) saveImage:(UIImage *)image withFileName:(NSString *)imageName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath { 
    if ([[extension lowercaseString] isEqualToString:@"png"]) { 
     [UIImagePNGRepresentation(image) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"png"]] options:NSAtomicWrite error:nil]; 
    } else if ([[extension lowercaseString] isEqualToString:@"jpg"] || [[extension lowercaseString] isEqualToString:@"jpeg"]) { 
     [UIImageJPEGRepresentation(image, 1.0) writeToFile:[directoryPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.%@", imageName, @"jpg"]] options:NSAtomicWrite error:nil]; 
    } else { 
     ALog(@"Image Save Failed\nExtension: (%@) is not recognized, use (PNG/JPG)", extension); 
    } 
} 

要保存圖像,只需像這樣做:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
[self saveImage:yourImage withFileName:@"Your Image Name" ofType:@"png" inDirectory:path]; 

要加載的圖像,實現此方法:

-(UIImage *) loadImage:(NSString *)fileName ofType:(NSString *)extension inDirectory:(NSString *)directoryPath { 

    UIImage * result = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/%@.%@", directoryPath, fileName, extension]]; 

    return result; 
} 

,然後用它是這樣的:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
yourImage.image = [self loadImage:@"Your Image Name" ofType:@"png" inDirectory:path]; 

或者你可以簡單地設置你的形象等於什麼該方法返回而不是創建一個方法:

NSString * path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]; 
yourImage.image = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/Your Image Name.png", path]]; 
相關問題