繁体   English   中英

上载图像以解析数据库

[英]Uploading an image to parse database

因此,这是我到目前为止尝试过的:

我建立了一些可以拍摄照片的代码(仅重要片段):

- (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];
}

然后,当您单击一个按钮时,我希望它上载到数据库。 这是我尝试过的:

- (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]);
        }
    }];
}

像这样在分析中制作了selfie1地方: 在此处输入图片说明


但是我遇到一个错误: 由于未捕获的异常'NSInvalidArgumentException',终止了应用程序,原因:'-[UIButton length]:无法识别的选择器发送到实例

有什么想法吗?


断点通知我:

在此处输入图片说明

还有这些: 在此处输入图片说明

我注意到了这一点:

在此处输入图片说明

而这来自调试:

在此处输入图片说明


我如何将upload_selfie连接到按钮

在此处输入图片说明


未声明的标识符错误照片:

在此处输入图片说明

我做了几次尝试。 它可能需要一些工作/测试,但是:这可能是一个解决方案:
更改:

- (IBAction)upload_selfie:(NSData *)data

至:

- (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.
    }
}

[self upload_selfie:nil withData:imageData];调用它[self upload_selfie:nil withData:imageData];

问题:为什么?
如果调用IBAction方法,则将与它一起sender
记住,我们通常写-(IBAction)actionMethod:(id)sender 如果要检查发件人,可以使用UIButton: UIButton *button = (UIButton *)sender;
因此,据我了解,如果通过用户交互(通过使用[self actionMethod:mySender]反对代码的调用)进行调用,则这种方法将把第一个参数作为发送者。
但是我不明白的是为什么您一开始将它作为IBAction来使用,却似乎没有链接到“直接用户操作”。

编辑:
经过更多的解释,我建议这样做:添加:
@property (nonatomic, strong) NSData *imageDataToSend;
替换[self upload_selfie:imageData]; 与: [self setImageDataToSend:imageData];
替换PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:data]; 其中: PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:imageDataToSend];
-(IBAction)upload_selfie:(id)sender-(IBAction)upload_selfie:(UIButton*)button替换-(IBAction)upload_selfie:(NSData *)data ,因为您似乎很困惑。
您可能要添加的内容:发送后,将self.imageDataToSendnil 检查IBAction方法的开头是否imageDataToSend不为nil 我不知道您所有的代码如何工作,但是一旦您单击了upload_selfie您可能还希望禁用该按钮,以防用户多次单击(并且您发送了许多不必要的相同图像),并且“发送完成后(使用[button:setEnabled:TRUE/FALSE]; )。

这并不是真正的答案,但我将其作为突出显示另一个错误的答案。

您的错误消息指出,向UIButton类型的对象发送的消息是“长度”,而没有该消息。 我看不到任何可能导致此问题的代码,因此我相信该错误来自其他地方。 请添加整个堆栈跟踪,也许我也可以使用其他错误的解决方案来更新答案。

但是您的代码还有另一个错误,在这里:

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

您已经将imageFile设置为图像。 这应该是

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

或类似。

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) {

        }];
    })

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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