Getting NSData from an NSURL

后端 未结 1 883
青春惊慌失措
青春惊慌失措 2021-01-17 02:49

I am trying to upload a photo from my app into a web service.

The flow I am attempting to create is as follows:

  1. User takes photo with
相关标签:
1条回答
  • 2021-01-17 03:41

    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]; 
    
    }
    
    0 讨论(0)
提交回复
热议问题