2

I have a urls like

"assets-library://asset/asset.PNG?id=2B9DB56C-B9C6-4F4E-AD51-8A5E5F1DD2AA&ext=PNG"

in "images" array. How i can get NSData from media, by this url? I need use NSData in this code:

#pragma mark VK methods

+(NSMutableArray*)attachmentIds:(NSArray*)images forMe:(NSDictionary*)me{
    NSMutableArray *attachmentsList = [NSMutableArray new];

    for (int i = 0; i<images.count; i++){

        NSData *imageData = [NSData dataWithContentsOfURL:[images objectAtIndex:i] ];

        NSString *serverUrl = [self getServerForVKUploadPhotoToWall:me];
        NSDictionary *uploadResult = [self sendVKPOSTRequest:serverUrl withImageData:imageData];
        NSString *hash = [uploadResult objectForKey:@"hash"];
        NSString *photo = [uploadResult objectForKey:@"photo"];
        NSString *server = [uploadResult objectForKey:@"server"];
        NSString *attach_id = [self getVKAttachIdforUser:me photo:photo server:server hash:hash];
        [attachmentsList addObject:attach_id];
    }
    return attachmentsList;
}

But NSData *imageData = [NSData dataWithContentsOfURL:[images objectAtIndex:i]]; isn't working;

Alex
  • 2,100
  • 19
  • 26

2 Answers2

2

I solved my question. Thanks https://www.cocoacontrols.com/controls/doimagepickercontroller

- (NSData *)getCroppedData:(NSURL *)urlMedia
{
    __block NSData *iData = nil;
    __block BOOL bBusy = YES;

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *representation = myasset.defaultRepresentation;
        long long size = representation.size;
        NSMutableData *rawData = [[NSMutableData alloc] initWithCapacity:size];
        void *buffer = [rawData mutableBytes];
        [representation getBytes:buffer fromOffset:0 length:size error:nil];
        iData = [[NSData alloc] initWithBytes:buffer length:size];
        bBusy = NO;
    };

    ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror)
    {
        NSLog(@"booya, cant get image - %@",[myerror localizedDescription]);
    };

    [_assetsLibrary assetForURL:urlMedia
                    resultBlock:resultblock
                   failureBlock:failureblock];

    while (bBusy)
        [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];

    return iData;
}
Alex
  • 2,100
  • 19
  • 26
  • Thanks. I tried lots of examples to get the bytes of a video file and kept coming up empty. Your example helped. – Mark.ewd Nov 08 '14 at 15:03
1

I think you can look at this question. This will allow you to retrieve a UIImage from the given asset URL, which can then be converted into NSData.

The Asset URL is not a file URL, so the dataWithContentsOfURL fails.

Community
  • 1
  • 1
Tcharni
  • 644
  • 1
  • 6
  • 17