如何知道我的照片库中保存的照片的URL?

时间:2013-10-24 19:14:39

标签: ios objective-c url uiimagepickercontroller

我正在开发一款简单的益智游戏,它使用图像作为棋盘游戏的背景。 一切都很好,但有一点困扰我。当我从相机拍摄照片时,我可以将其保存在我的照片库中,但我不知道它保存在哪里,我的意思是,我的照片的确切网址是什么。

以下是我保存照片的代码:

- (void) imagePickerController:(UIImagePickerController *) picker didFinishPickingMediaWithInfo:(NSDictionary *) info
{
    NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
    UIImage *originalImage, *editedImage, *imageToSave;

    // Handle a still image capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0) == kCFCompareEqualTo) {

        editedImage = (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
        originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];

        if (editedImage) {
            imageToSave = editedImage;
        } else {
            imageToSave = originalImage;
        }

        CGRect bounds;

        bounds.origin = CGPointZero;
        bounds.size = imageToSave.size;

        [scrollView setContentSize:bounds.size];
        [scrollView setContentOffset:bounds.origin];
        [imageView setFrame:bounds];
        imageView.image = imageToSave;

        if ([info objectForKey:@"UIImagePickerControllerReferenceURL"] == nil) {
            // Save the new image (original or edited) to the Camera Roll
            ALAssetsLibrary *al = [[ALAssetsLibrary alloc] init];
            ALAssetOrientation orientation; //= [[[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"] integerValue];
            NSString *infoOrientation = [[info objectForKey:@"UIImagePickerControllerMediaMetadata"] objectForKey:@"Orientation"];
            switch ([infoOrientation integerValue]) {
                case 3:
                    orientation = ALAssetOrientationUp;
                    break;
                case 6:
                    orientation = ALAssetOrientationRight;
                    break;
                default:
                    orientation = ALAssetOrientationDown;
                    break;
            }
            [al writeImageToSavedPhotosAlbum:[imageToSave CGImage] orientation:orientation completionBlock:^(NSURL *assetURL, NSError *error) {
                if (error == nil) {
                    NSLog(@"saved");
                    savedImageCam = assetURL;
                } else {
                    NSLog(@"error");
                }
            }];
        } else {
            NSLog(@"URL from saved image: %@", savedImageCam);
            NSLog(@"URL from photo image: %@", [info objectForKey:@"UIImagePickerControllerReferenceURL"]);
        }
    }

    // Handle a movie capture
    if (CFStringCompare ((CFStringRef) mediaType, kUTTypeMovie, 0) == kCFCompareEqualTo) {

        NSString *moviePath = (NSString *)[[info objectForKey:UIImagePickerControllerMediaURL] path];

        if (UIVideoAtPathIsCompatibleWithSavedPhotosAlbum (moviePath)) {
            UISaveVideoAtPathToSavedPhotosAlbum (moviePath, nil, nil, nil);
        }
    }

    [self dismissViewControllerAnimated:YES completion:nil];
}

调用方法UIImageWriteToSavedPhotosAlbum不返回URL,如果我直接通过相机调用照片库,则实例变量info只会带来URL。

有人知道我该怎么办?

1 个答案:

答案 0 :(得分:0)

请勿使用UIImageWriteToSavedPhotosAlbum。相反,请使用ALAssetsLibrarywriteImageToSavedPhotosAlbum:orientation:completionBlock:。然后,completionBlock可让您访问资产网址,以便日后再次使用该网址。