3

ALAssetLibraryを使用してiPhoneの写真フォルダから画像を取得するアプリケーションを作成しました。位置情報サービスを使用せずにAlAssetLibraryを使用してファイルを取得できますか?AlAssetLibraryで位置情報サービスを回避するにはどうすればよいですか?

4

2 に答える 2

3

現在、位置情報サービスを使用せずにALAssetLibraryにアクセスする方法はありません。この問題を回避するには、はるかに限定されたUIImagePickerControllerを使用する必要があります。

于 2011-04-19T03:17:19.103 に答える
1

ライブラリからの画像が1つだけ必要な場合、上記の答えは正しくありません。たとえば、ユーザーにアップロードする写真を選択してもらう場合です。この場合、ロケーション権限を必要とせずに、ALAssetLibraryを使用してその単一の画像を取得できます。

これを行うには、UIImagePickerControllerを使用して画像を選択します。UIImagePickerControllerReferenceURLUIImagePickerControllerが提供するが必要です。

NSDataこれには、変更されていないオブジェクトへのアクセスを提供し、アップロードできるという利点があります。

後でファイルを使用して画像を再エンコードするUIImagePNGRepresentation()UIImageJPEGRepresentation()、ファイルのサイズを2倍にすることができるため、これは便利です。

ピッカーを提示するには:

picker = [[UIImagePickerController alloc] init];
[picker setDelegate:self];
[picker setSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
[self presentViewController:picker animated:YES completion:nil];

画像やデータを取得するには:

- (void)imagePickerController:(UIImagePickerController *)thePicker didFinishPickingMediaWithInfo:(NSDictionary *)info
{   
    [picker dismissViewControllerAnimated:YES completion:nil];
    NSURL *imageURL = [info objectForKey:@"UIImagePickerControllerReferenceURL"];

    ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];

    [assetLibrary assetForURL:imageURL
                  resultBlock:^(ALAsset *asset) {
                      // get your NSData, UIImage, or whatever here
                     ALAssetRepresentation *rep = [self defaultRepresentation];
                     UIImage *image = [UIImage imageWithCGImage:[rep fullScreenImage]];

                     Byte *buffer = (Byte*)malloc(rep.size);
                     NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:rep.size error:nil];
                     NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];

                     if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
                         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
                     }
                 }
                 failureBlock:^(NSError *err) {
                     // Something went wrong; get the image the old-fashioned way                            
                     // (You'll need to re-encode the NSData if you ever upload the image)
                     UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];

                     if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
                         UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
                     }
                 }];
}
于 2013-02-07T01:27:05.023 に答える