-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

好的,所以我通过删除“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 回答