0

写真を撮り、ファイルをUIImageviewとdocumentsフォルダーの両方に保存して、後でUIImageviewとして呼び出すことができるようにする簡単なアプリを作成しようとしています。アプリが実際にファイルを作成しているかどうかを確認するために、アプリケーションが.plistファイル内でのiTunesファイル共有をサポートするようにしました。

問題は、生成されたファイルがゼロバイトであるということです。PNGとJPGの両方の形式を試しました。

- (IBAction)picImage:(id)sender {
imagePicker = [[UIImagePickerController alloc] init];
imagePicker.delegate = self;

//check to see if the device has camera capability
if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
{
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;

}
else
{
    imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
}

[self presentViewController:imagePicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    //set the UIImageView to the selected image
    playerImage.image = [info objectForKey:UIImagePickerControllerOriginalImage];

    //obtaining saving path
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:@"player.png"];

    UIImage *editedImage = [UIImage imageWithContentsOfFile:imagePath];
    NSData *imageData = UIImagePNGRepresentation(editedImage);
    //NSData *imageData = UIImageJPEGRepresentation(editedImage,1.0);

    // This actually creates the image at the file path specified, with the image's NSData.
   [[NSFileManager defaultManager] createFileAtPath:imagePath contents:imageData attributes:nil];

    //dismiss the imagepicker view controller
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{
[self dismissViewControllerAnimated:YES completion:nil];
}
4

1 に答える 1

1

このコードを試して、機能しているかどうかを知らせてください.. !!!!

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *strimagename;
    strimagename=[NSString stringWithFormat:@"Test.jpg"];


NSString *thumbFilePath = [documentsDirectory stringByAppendingPathComponent:strimagename];
UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
NSData *thumbData = UIImageJPEGRepresentation(image, 1);
[thumbData writeToFile:thumbFilePath atomically:YES];
[imgPicker dismissModalViewControllerAnimated:YES];

このコードは画像をドキュメンタリーフォルダに保存します。

ハッピーコーディング..!!!!!!

于 2012-11-01T12:51:35.627 に答える