2012-03-30 34 views
0

実行時にはどちらが良いかを指定します。 1)サイズ250X120の画像をsqliteに保存します。 2)ドキュメントディレクトリに同じサイズのイメージを保存する。 2つの場所では、これらの保存された画像をコントロールに表示する必要があります。 また、最大20枚の画像を表示する必要があります。実行時に画像をiphoneアプリケーションに保存

答えて

1

2番目のオプションは、最初のオプションよりはるかに優れています。第二部の使用を達成するには、次の

- (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

1

1 M.Sharjeelを - と、私たちが使用して、我々は(携帯電話/パッド上のsqliteに裏打ちされた)コアデータオブジェクトを持っています半ハイブリッド、通常であります検索が高速なファイルに関するメタデータを持っていて、NSStringをdocumentsDirectory内のパスに保存します。

+0

コア・データは、効率的で、SQLiteのより良い実行...彼と一緒に同意します –

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]]; 
関連する問題