10

アプリでELCImagePickerControllerを使用していますが、選択した fullScreenImage を配列に保存したくありません。40 個の iPad 画像を選択した場合、それは良くないからです。

method の dict からではUIImagePickerControllerReferenceURLなく、からデータを取得したい。UIImagePickerControllerOriginalImage- (void)elcImagePickerController:(ELCImagePickerController *)picker didFinishPickingMediaWithInfo:(NSArray *)info

私が試してみました:

NSDictionary *dict = [info objectAtIndex:count];            

NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",[dict objectForKey:@"UIImagePickerControllerReferenceURL"]]]];//UIImagePNGRepresentation([UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@",[dict objectForKey:@"UIImagePickerControllerReferenceURL"]]] );
        NSLog(@"length %d",[data length]);
        imageview.image = [UIImage imageWithData:data];

ただし、毎回0バイトを取得しています。フォーラムで利用可能なすべての回答を試しましたが、役に立ちません。

誰でもこれに答えてもらえますか?

4

3 に答える 3

26

UIImagePickerControllerReferenceURLNSURL文字列オブジェクトではなくオブジェクトを返します。コードを次のように変更してください -

NSData *data = [NSData dataWithContentsOfURL:[dict objectForKey:@"UIImagePickerControllerReferenceURL"]];
NSLog(@"length %d",[data length]);
imageview.image = [UIImage imageWithData:data];

UIImagePickerControllerReferenceURLNSURLのオブジェクトを返すためAssets Library、次のように画像を取得できます-

ALAssetsLibrary *assetLibrary=[[ALAssetsLibrary alloc] init];
[assetLibrary assetForURL:[[self.imagedata objectAtIndex:i] valueForKey:UIImagePickerControllerReferenceURL] resultBlock:^(ALAsset *asset) {
    ALAssetRepresentation *rep = [asset defaultRepresentation];
    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];//this is NSData may be what you want
    [data writeToFile:photoFile atomically:YES];//you can save image later
} failureBlock:^(NSError *err) {
    NSLog(@"Error: %@",[err localizedDescription]);
}];

注: ALAssetsLibrary は iOS 9 で廃止されました。

于 2012-05-29T12:59:01.810 に答える
1

ELCImagePickersの「選択したアセット」でできること

-(void)selectedAssets:(NSArray*)_assets {

"... your code .?.?."

    NSMutableArray *returnArray = [[NSMutableArray alloc] init];

    for(ALAsset *asset in _assets) {

        NSMutableDictionary *workingDictionary = [[NSMutableDictionary alloc] init];
        [workingDictionary setObject:[[asset valueForProperty:ALAssetPropertyURLs] valueForKey:[[[asset valueForProperty:ALAssetPropertyURLs] allKeys] objectAtIndex:0]] forKey:@"UIImagePickerControllerReferenceURL"];

".. any other properties you need ?"

        [returnArray addObject:workingDictionary];

    }
}

次に、他のクラスで配列から保存します

- (void) importImagesFromArray:(NSArray *)_images toFolder:(NSString *)folderPath
{
   if ([_images count] > 0) {

    //... YOUR CODE HERE FOR CHECKING YOUR ARRAY...Maybe a loop or something//
    //
    //
    //

    //ex:

ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
    for (NSDictionary *dict in _images) {



        [library assetForURL:[dict objectForKey:@"UIImagePickerControllerReferenceURL"]
                 resultBlock:^(ALAsset *asset){

                     //You Can Use This

                     UIImage *theImage = [UIImage imageWithCGImage:[[asset defaultRepresentation] fullResolutionImage]
                                                             scale:1.0
                                                       orientation:[[asset valueForProperty:@"ALAssetPropertyOrientation"] intValue]];

                     //[....save image blah blah blah...];

                     ///////////////////////////////////////////////////
                     ///////////////////////////////////////////////////

                     ////// OR YOU CAN USE THIS////////////////////

                     ALAssetRepresentation *rep = [asset defaultRepresentation];
                     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];//this is NSData may be what you want
                     [data writeToFile:[folderPath stringByAppendingPathComponent:@"Some Filename You Need To Assign"] atomically:YES];


                 }

                failureBlock:^(NSError *error){
                    NSLog(@"Error saving image");

                }];

        // Dont forget to release library

    }
}
}
于 2013-01-07T18:27:47.040 に答える