How to upload image that was taken from UIImagePickerController

后端 未结 6 922
离开以前
离开以前 2020-12-17 04:41

After user choose image from the iPhone library with UIImagePickerController, I want to upload it to my server using ASIHTTPRequest library.

6条回答
  •  孤城傲影
    2020-12-17 04:47

    There are two ways

    1:

    You can upload the image using the imagePickerController delegate

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{   
    
        UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
        //Upload your image
    }
    

    2:

    You can save the picked image url and upload later using this

    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    
        NSString *imageUrl = [NSString stringWithFormat:@"%@",[info valueForKey:UIImagePickerControllerReferenceURL]];
        //Save the imageUrl
    }
    
    -(void)UploadTheImage:(NSString *)imageUrl{
    
     NSURL *url = [[NSURL alloc] initWithString:imageUrl];
     typedef void (^ALAssetsLibraryAssetForURLResultBlock)(ALAsset *asset);
     typedef void (^ALAssetsLibraryAccessFailureBlock)(NSError *error);    
    
     ALAssetsLibraryAssetForURLResultBlock resultblock = ^(ALAsset *myasset){
    
      ALAssetRepresentation *rep = [myasset defaultRepresentation];
      CGImageRef iref = [rep fullResolutionImage];
      UIImage *myImage = nil;   
    
      if (ref) {
          myImage = [UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
    
            //upload the image   
         }      
      };      
    
      ALAssetsLibraryAccessFailureBlock failureblock  = ^(NSError *myerror){
    
      };          
    
    
      ALAssetsLibrary* assetslibrary = [[[ALAssetsLibrary alloc] init] autorelease];
     [assetslibrary assetForURL:url resultBlock:result block failureBlock:failureblock];    
    

    }

    Notes: Please make sure the scope of ALAssetsLibrary object while using ARC.Better to use the ALAssetsLibrary object as a singleton.

提交回复
热议问题