私が取り組んでいるアプリケーションでは、カメラロールから画像を選んでUIImageViewに表示することができます。今、私はデータを保存しようとするいくつかの問題があります。私はイメージをPNGに変換してデータを保存する必要があることを知っていますが、それを行うにはいくつか問題があります。私はファイルセーブとObjective-Cのために非常に新しいです。 。iOS:イメージをPNGに変換して保存できるようにする
- (NSString *)dataFilePath {
NSArray *paths = NSSearchPathForDirectoriesInDomains(
NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
return [documentsDirectory stringByAppendingPathComponent:kFilename9];
}
- (void)applicationDidEnterBackground:(NSNotification *)notification {
NSMutableArray *array = [[NSMutableArray alloc] init];
NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image.image)];
[array addObject:imageData];
[array writeToFile:[self dataFilePath] atomically:YES];
}
- (void)viewDidLoad
{
self.imgPicker = [[UIImagePickerController alloc] init];
self.imgPicker.allowsImageEditing = YES;
self.imgPicker.delegate = self;
self.imgPicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
image.image = [array objectAtIndex:0];
}
UIApplication *app = [UIApplication sharedApplication];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationDidEnterBackground:)
name:UIApplicationDidEnterBackgroundNotification
object:app];
[super viewDidLoad];
}
プログラムは、単にこれはで行われることのタブをタップするとクラッシュしたデバッガの状態 "『NSInvalidArgumentExceptionを』、理由:ここで私が使用しているコードは「 - [__ NSCFData _isResizable]:認識されていないセレクタインスタンス0x16cf50 ''に送信されました。どんな助けでも大歓迎です、ありがとう! yourUIImageを提供
素晴らしいです、ありがとうございます! – John
問題ありません。喜んで – David
画像は保存されませんが、コードを編集して保存する方法を知っていますか?もう一度ありがとう! – John