0

以下のコードを使用して、選択した画像をドキュメント ディレクトリに保存しました。

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{

    UIImage *img = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];

    NSData *imageData = UIImagePNGRepresentation(img);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd,HH:mm:ss"];
    NSString *imageName = [NSString stringWithFormat:@"photo-%@.png",
                           [dateFormatter stringFromDate:[NSDate date]]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPathToFile atomically:YES];
       imageView.image=image;
        NSLog(@"path %@",fullPathToFile);// shows the complete image path
    [self dismissModalViewControllerAnimated:YES];
}

しかし、残念ながら、選択した画像がドキュメント ディレクトリに表示されません。'fullPathToFile' 変数のコンテンツをブラウザーの URL として使用して、それが利用可能かどうかを確認しました..しかし、そうではありません..あなたの助けを求めています.

4

1 に答える 1

0

これを試して :-

- (void)imagePickerController:(UIImagePickerController *)picker
        didFinishPickingImage:(UIImage *)image
                  editingInfo:(NSDictionary *)editingInfo
{
    [self dismissModalViewControllerAnimated:YES];
    UIImage *img = [editingInfo objectForKey:UIImagePickerControllerOriginalImage];

    NSData *imageData = UIImagePNGRepresentation(image);
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateStyle:NSDateFormatterShortStyle];
    [dateFormatter setDateFormat:@"yyyy-MM-dd,HH:mm:ss"];
    NSString *imageName = [NSString stringWithFormat:@"photo-%@.png",
                           [dateFormatter stringFromDate:[NSDate date]]];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *fullPathToFile = [documentsDirectory stringByAppendingPathComponent:imageName];
    [imageData writeToFile:fullPathToFile atomically:YES];
       imageView.image=image;
        NSLog(@"path %@",fullPathToFile);// shows the complete image path

}

それがあなたを助けることを願っています..

于 2013-02-11T11:20:43.407 に答える