-1

撮影した画像をアプリケーションドキュメントディレクトリに保存したかったのですが、どういうわけか、カウンターは常に上がっていますが、ディレクトリ内に画像がありません。問題であると想定されるのは?

どうもありがとう!

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [picker dismissModalViewControllerAnimated:YES];
    self.imageViewRecipt.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    //
    // Getting the current counter value
    //
    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];

    int imageCounter;
    imageCounter = [[prefs objectForKey:@"imageCounter"]intValue];

    //
    // Obtaining saving path
    //
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%i.png",imageCounter]];

    //
    // Extracting image from the picker and saving it
    //
    NSString *mediaType = [info objectForKey:UIImagePickerControllerMediaType];
    if ([mediaType isEqualToString:@"public.image"])
    {
        UIImage *editedImage = [info objectForKey:UIImagePickerControllerEditedImage];
        NSData *webData = UIImagePNGRepresentation(editedImage);
        [webData writeToFile:imagePath atomically:YES];

        //
        // Saving the new counter into the plist
        //
        imageCounter++;

        [prefs setInteger:imageCounter forKey:@"imageCounter"];
        [prefs synchronize];
    }
}
4

1 に答える 1

0

OK、「mediaType」とifステートメントを削除して修正し、次のように置き換えました。

- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
    [picker dismissModalViewControllerAnimated:YES];
    self.imageViewRecipt.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];

    //
    // Getting the current counter value
    //
    NSUserDefaults * prefs = [NSUserDefaults standardUserDefaults];

    int imageCounter;
    imageCounter = [[prefs objectForKey:@"imageCounter"]intValue];

    //
    // Obtaining saving path
    //
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *imagePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"image%i.png",imageCounter]];

    //
    // Extracting image from the picker and saving it
    //
    UIImage *editedImage = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];
    NSData *webData = UIImagePNGRepresentation(editedImage);
    [webData writeToFile:imagePath atomically:YES];

    //
    // Saving the new counter into the plist
    //
    imageCounter++;

    [prefs setInteger:imageCounter forKey:@"imageCounter"];
    [prefs synchronize];
}

そして今、それはうまく機能しています。

于 2012-08-07T07:29:55.270 に答える