0

I'm currently having some trouble with data getting lost when transferring from a ViewController to a subclass of PFFile. The data being passed is image data to upload to a users profile. Here's the code for selecting the image:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info { // Access the uncropped image from info dictionary UIImage *image = [info objectForKey:@"UIImagePickerControllerOriginalImage"]; // Dismiss controller [picker dismissViewControllerAnimated:YES completion:nil]; // Resize image _focusedImage.image = image; NSData *imageData = UIImageJPEGRepresentation(image, 0.05f); PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:imageData]; [[imageUpload uploadImage] setImagePFFile:imageFile]; } 

The Log on imageFile in this view is printing out correctly. However, when I pass the data through to my singleton class imageUpload uploadImage This is what the data structure looks like:

+ (imageUpload *) uploadImage { static imageUpload*_sharedImageUpload = nil; _sharedImageUpload = [[self alloc] init]; _sharedImageUpload.imageData = [[NSData alloc] init]; PFUser *user = [PFUser currentUser]; _sharedImageUpload.imagePFFile = [[PFFile alloc] init]; PFFile *imageFile = [PFFile fileWithName:@"Image.jpg" data:_sharedImageUpload.imageData]; [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { [user setObject:imageFile forKey:@"image"]; [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { NSLog(@"This should be the profile image upload"); } else { NSLog(@"Something went wrong: %@", error); } }]; } }]; return _sharedImageUpload; } 

When I get to this point, the system just uploads a blank file (zero bytes) to Parse. The naming is right and its going in the right place on the database, but somewhere along the line the data in the file itself is being lost. I can't figure out why. Does anyone have any suggestions?

1
  • @jefflovejapan I'm still relatively new to this; how do you mean? Commented Sep 26, 2014 at 15:44

1 Answer 1

6

It looks like you're confusing objects and methods. What you want is a singleton object that has a method / function that uploads your image. I think this is what you're looking for:

//ImageUploader.h #import <Foundation/Foundation.h> @interface ImageUploader : NSObject + (instancetype)uploader; - (void)uploadImageFile:(PFFile *)aFile; @end //ImageUploader.m #import "ImageUploader.h" @implementation ImageUploader + (instancetype)uploader { static ImageUploader * _uploader = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ _uploader = [[self alloc] init]; }); return _uploader; } -(void)uploadPFFile:(PFFile *)imageFile{ [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { [user setObject:imageFile forKey:@"image"]; [user saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) { if (!error) { NSLog(@"This should be the profile image upload"); } else { NSLog(@"Something went wrong: %@", error); } }]; } }]; } @end 

You invoke it by calling [[ImageUploader uploader]uploadImageFile:someFile].

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. You're right, I was confusing objects and methods for a second there. Much appreciated!
No problem. I just realized that the saveInBackground method is defined on PFFile, which means your uploader isn't really doing anything. Instead, you could just call saveInBackground on the file when you first create it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.