Getting NSData from an NSURL

孤人 提交于 2019-12-01 14:25:52
Shamsudheen TK

The only way is You can retrieve the UIImage from Photo-Library using ALAsset URL and convert in to NSData.

Add ALAssetLibrary

Import ALAssetLibrary header file

-(void)uploadItemAtIndex:(NSUInteger)index 
       withProgressBlock:(progressBlock)pBlock 
           withExitBlock:(exitBlock)eBlock
{  

    ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset)
    {
        ALAssetRepresentation *rep;
        if([myasset defaultRepresentation] == nil) {
            return;
        } else {
            rep = [myasset defaultRepresentation];
        }
        CGImageRef iref = [rep fullResolutionImage];

        dispatch_sync(dispatch_get_main_queue(), ^{


            UIImage *myImage = [UIImage imageWithCGImage:iref];            
            NSData *data = //convert the myImage to NSData

            BCard *item = [uploadQueue objectAtIndex:index];
            NSURL *url = [NSURL URLWithString:@"http://192.168.0.116:8080"];  
            AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
            numberedName = numberedName +1;
            NSString *name = [NSString stringWithFormat:@"%d",numberedName];



            //upload data using AFNetworking here


        });//end block


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

    NSURL *asseturl = 
        [NSURL URLWithString:[self.photoPath objectAtIndex:[arrayIndex intValue] ]];
    //using ARC , you have to declare ALAssetsLibrary as member variable 
    ALAssetsLibrary* assetslibrary = [[ALAssetsLibrary alloc] init]; 

    [assetslibrary assetForURL:assetURL 
                   resultBlock:resultblock
                  failureBlock:failureblock]; 

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!