图像不会保存到文档目录中

时间:2012-08-07 05:10:36

标签: iphone objective-c ios xcode uiimagepickercontroller

我想将已经拍摄的图像保存到应用程序文档目录中,但由于某种原因,计数器总是在上升,但目录中没有图片。什么似乎是问题?

非常感谢!

- (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];
    }
}

1 个答案:

答案 0 :(得分:0)

好的,所以我通过删除“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];
}

现在它的工作正常。