简体   繁体   中英

Uploading an image to parse database

So this is what I have tried so far:

I have built a bit of code that takes a photo (only the important snippet):

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {

    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    self.imageView.image = chosenImage;
    _camera_button.hidden = true;
    _camera_imageview.hidden = false;
    [_next_camera setEnabled:YES];
    NSData *imageData = UIImagePNGRepresentation(chosenImage);
    [self upload_selfie:imageData];

    [picker dismissViewControllerAnimated:YES completion:NULL];
}

And then when you click on a button I want it to upload to the database. This is what I have tried:

- (IBAction)upload_selfie:(NSData *)data{
    PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:data];
    [imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (!error) {
            // Hide old HUD, show completed HUD (see example for code)

            // Create a PFObject around a PFFile and associate it with the current user
            PFObject *selfie = [PFObject objectWithClassName:@"selfie1"];
            [selfie setObject:imageFile forKey:@"imageFile"];


            PFUser *user = [PFUser currentUser];
            [selfie setObject:user forKey:@"imageFile"];

            [selfie saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (!error) {
                }
                else{
                    NSLog(@"Error: %@ %@", error, [error userInfo]);
                }
            }];
        }
        else{
            NSLog(@"Error: %@ %@", error, [error userInfo]);
        }
    }];
}

Where selfie1 is made on parse like this: 在此处输入图片说明


But I am getting an error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton length]: unrecognized selector sent to instance

Any ideas why?


The break point notified me of this:

在此处输入图片说明

And these: 在此处输入图片说明

And I have noticed this:

在此处输入图片说明

And this from the debugging:

在此处输入图片说明


How i connect upload_selfie to the button

在此处输入图片说明


Photo of undeclared identifier error:

在此处输入图片说明

I did a few try. It may need a few works/tests, but: Here could be a solution:
Change:

- (IBAction)upload_selfie:(NSData *)data

To:

- (IBAction)upload_selfie:(id)sender withData:(NSData *)data
{
    if ([data isKindOfClass:[NSData class]])
    {
    //Put the rest of your code, it what would called manually
    }
    else
    {
        // User clicked on the button, but didn't "send" data with it.
    }
}

Call it with [self upload_selfie:nil withData:imageData];

The question: Why?
If you call an IBAction method, you'll send with it the sender .
Remember, we usually write: -(IBAction)actionMethod:(id)sender . If you want to check the sender, you may do, in case of a UIButton: UIButton *button = (UIButton *)sender;
So, from what I understood, this kind of method will take the first parameter as the sender if it's called from an user interaction (by opposition of a call by code with [self actionMethod:mySender] .
But a thing I don't understand is why you put it as a IBAction at first, and seem to link no "direct user action" to it.

Edit:
After more explanation, I'd suggest this: Add:
@property (nonatomic, strong) NSData *imageDataToSend;
Replace [self upload_selfie:imageData]; with: [self setImageDataToSend:imageData];
Replace PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:data]; with: PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:imageDataToSend];
Replace -(IBAction)upload_selfie:(NSData *)data with -(IBAction)upload_selfie:(id)sender or -(IBAction)upload_selfie:(UIButton*)button since your seems quite confused.
What you may add: put self.imageDataToSend to nil once it's send. Check at the beginning of the the IBAction method if imageDataToSend is not nil . I don't know how all of your code works, but once you clicked on upload_selfie you may also want to disable the button, in case of the user click many times (and you send unnecessary many times the sames image), and "reable" once the sending is done (with [button:setEnabled:TRUE/FALSE]; ).

This is not really an answer, but I put it as an answer to highlight another error.

Your error message states that an object of type UIButton is sent the message "length", which it doesn't have. I can't see anything in your code that could cause this, so I believe the error comes from elsewhere. Please add the whole stack trace, and maybe I can update the answer with a solution to the other error as well.

But your code has another error, and that is here:

PFUser *user = [PFUser currentUser];
[selfie setObject:user forKey:@"imageFile"];

You already set imageFile as the image. This should probably be

PFUser *user = [PFUser currentUser];
[selfie setObject:user forKey:@"user"];

or similar.

NSData* data = UIImageJPEGRepresentation(image, 0.3f);    
PFFile *imageFile = [PFFile fileWithName:[NSString  stringWithFormat:@"%f",[[NSDate date] timeIntervalSince1970]] data:data];
    TLPhoto *photo = [TLPhoto new];
    [photo setImageFile:imageFile];
    [photo setAlbum:album];
    [self setPermissionsForObject:photo];

    // Save the image to Parse
    dispatch_async(dispatch_get_main_queue(), ^{
        [photo save];
        [album.photos addObject:photo];
        [album saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {

        }];
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM