Search code examples
iosobjective-cwebnsdataquickblox

How to download an image using QuickBox API?


I'm trying to download an image file using Quickblox API. I can upload a file but when I try to download, NSData image doesn't show an image.

// Upload a user avatar, previously log in Quickblox API
                NSData *avatar = UIImagePNGRepresentation([UIImage imageNamed:@"userWhite"]);
                [QBRequest TUploadFile:avatar fileName:@"avatar.png" contentType:@"image/png" isPublic:YES successBlock:^(QBResponse *response, QBCBlob *blob) {
                    // Success
                } statusBlock:^(QBRequest *request, QBRequestStatus *status) {

                } errorBlock:^(QBResponse *response) {

                }];

Image Upload: Image

Download avatar:

 NSString* userProfilePictureID = [NSString stringWithFormat:@"%ld",(long)[[LocalStorageService shared] currentUser].blobID]; // user - an instance of QBUUser class
    // download user profile picture
    [QBRequest downloadFileWithUID:@"318547" successBlock:^(QBResponse *response, NSData *fileData) { UIImage* image = [UIImage imageWithData:fileData];

    } statusBlock:^(QBRequest *request, QBRequestStatus *status) {

    } errorBlock:^(QBResponse *response) {
    }];

UIImage doesn't show any image. What can I do? NSData isn't corrupted.


Solution

  • Your download code looks right. You basically should call QBRequest method and pass blob.UID to it. But in my case blob.UID is something like this 9357c3d66b944880a82cdbeb836f143c00. However there are steps I took to accomplish your task:

    1) Sign up or login user

    Either by calling +[QBRequest signUp:successBlock:errorBlock:] or +[QBRequest logInWithUserLogin:password:successBlock:errorBlock].

    2) Enumerate through all available blobs for current user

      [QBRequest blobsWithSuccessBlock:^(QBResponse* response,
                                         QBGeneralResponsePage* page,
                                         NSArray* blobs) {
          [self findAndShowAvatarFromBlobs:blobs];
      } errorBlock:^(QBResponse* response) { <...> }];
    

    3) Find desired blob and download it

    - (void)findAndShowAvatarFromBlobs:(NSArray*)blobs {
      for (QBCBlob* blob in blobs) {
        if ([blob.name hasPrefix:@"avatar"]) {
          [QBRequest downloadFileWithUID:blob.UID
              successBlock:^(QBResponse* response, NSData* fileData) {
                  UIImage* image = [UIImage imageWithData:fileData];
                  self.imageView.image = image;
              }
              statusBlock:^(QBRequest* request, QBRequestStatus* status) { <...> }
              errorBlock:^(QBResponse* response) { <...> }];
        }
      }
    }
    

    Probably you have a problem with getting valid blob UID (I suspect that you are using blob ID which is not the same as UID). Is it possible for you to use the same kind of logic?

    Full project you can find here: https://dl.dropboxusercontent.com/u/930742/so/QuickBloxSample.zip

    It creates necessary user, uploads and then downloads image with showing it in UIImageView demonstrating all described steps. (Don't forget to set your service and application keys in AppDelegate)

    P.S. In admin panel you can find blob UID by clicking on its ID in Content table click on ID

    here is UID