0

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

这是我保存照片的代码:

- (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。

有人知道我该如何解决吗?

4

1 回答 1

0

不要使用UIImageWriteToSavedPhotosAlbum. 相反,使用ALAssetsLibraryand writeImageToSavedPhotosAlbum:orientation:completionBlock:。然后completionBlock,您可以访问资产 URL,您可以使用该 URL 在将来再次获取图像。

于 2013-10-24T19:16:24.250 回答