あなたは単に行う、画像を保存するには、次のアプローチ
-(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]];
あなたは 'の戻り値を確認しましたwriteToFile:アトミック: '?エラーがある場合は、 'writeToFile:options:error:'バリアントを使用してエラーを確認することができます。 – MrTJ
また、実際にイメージをアプリケーションバンドルからドキュメントディレクトリにコピーする必要があるのはなぜですか? – MrTJ
あなたはデバイス内のコードをチェックしていますか? シミュレータはコードを1行ずつ実行しないため、[imageData writeToFile:fullPathToFile atomically:NO];メソッドはパスに画像を書き込むために時間がかかるので、しばらくしてから画像に画像をロードしてみてください –