Start a new topic

Unable to load image

I've started using Kinvey, iOS to be specific, before few days. However, I'm not successfull in fetching the images being uploaded. I'm uploading an image and saving it's ID and URL in Post object.


Below is the code for uploading image:


 NSData* data = UIImageJPEGRepresentation([imageViewPost image], 0.9); //convert to a 90% quality jpeg

 

 KCSMetadata *metadata = [KCSMetadata new];

 [metadata setGloballyReadable:YES];

 

 

 [KCSFileStore uploadData:data options:@{KCSFilePublic:@(YES),KCSFileACL:metadata,KCSFileMimeType:@"image/jpeg"} completionBlock:^(KCSFile *uploadInfo, NSError *error) {

 NSLog(@"Upload finished. File id='%@', error='%@'.", [uploadInfo fileId], error);

 

 if(!error && uploadInfo){

 Post *newPost = [Post new];

 newPost.postTitle = textFieldTitlew.text;

 newPost.postOwnerID = CURRENT_USER.userId;

 newPost.postOwnerName = CURRENT_USER.username;

 newPost.postDescription = textFieldDescription.text;

 newPost.postLink = textFieldLink.text;

 newPost.mediaURL = uploadInfo.remoteURL.absoluteString;

 newPost.postMediaID = [uploadInfo fileId];

 

 

 [KINVEY_MANAGER.postsStore saveObject:newPost withCompletionBlock:^(NSArray *objectsOrNil, NSError *errorOrNil) {

 

 [appDelegate stopLoadingView];


 if (errorOrNil != nil) {

 //save failed

 NSLog(@"Save failed, with error: %@", [errorOrNil localizedFailureReason]);

 [appDelegate showAlertWithMessage:[errorOrNil localizedDescription]];

 } else {

 //save was successful

 NSLog(@"Successfully saved event (id='%@').", [objectsOrNil[0] kinveyObjectId]);

 [appDelegate showAlertWithMessage:@"Post added successfully."];

 }

 


 } withProgressBlock:^(NSArray *objects, double percentComplete) {

 

 }];

 }else{

 [appDelegate stopLoadingView];

 }

 

 } progressBlock:nil];



I'm unable to load the image using the post.mediaURL property. I want to load the image using AFNetworking, that's why I'm saving the URL. I tried below code as well:


 [KCSFileStore downloadFileByName:post.postMediaID completionBlock:^(NSArray *downloadedResources, NSError *error) {

 

 if(!error && downloadedResources.count>0){

 KCSFile* file = downloadedResources[0];

 NSURL* fileURL = file.localURL;

 UIImage* image = [UIImage imageWithContentsOfFile:[fileURL path]]; //note this blocks for awhile

 imageViewPostImage.image = image;

 }

 

 } progressBlock:^(NSArray *objects, double percentComplete) {

 

 }];



But always get downloadedResources as "0 objects" and no error at all.

Please help me fix this. Thanks in advance.

png

1 person has this question

 Mark,


Can you please check if the file upload was successful and that the file exists there (and its name) - you can do this from Kinvey Console->Data and then select “Files” in the dropdown.

__NOTE for downloadFileByName:
__ If there is no matching file by the specified name, the result will be an empty `downloadedResources` array, and NOT a 404 error. This is a change of behavior from previous versions.


Thanks,

Pranav

Kinvey Support

 Mark,


Just wanted to check with you if your issue is solved with my suggestion. If not, please let us know so that we can further help you.


Thanks,

Pranav

Kinvey Support

Login or Signup to post a comment