gpt4 book ai didi

ios - 上传图像以解析数据库

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:05:36 27 4
gpt4 key购买 nike

所以这就是我到目前为止所尝试的:

我编写了一些拍照代码(只有重要的片段):

- (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 是这样解析的: enter image description here


但我收到一个错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[UIButton 长度]:无法识别的选择器发送到实例

有什么想法吗?


断点通知我:

enter image description here

还有这些: enter image description here

我注意到了这一点:

enter image description here

这来自调试:

enter image description here


我如何将upload_selfie 连接到按钮

enter image description here


未声明标识符错误照片:

enter image description here

最佳答案

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

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

问题:为什么?
如果您调用 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"数据:图像数据发送];
-(IBAction)upload_selfie:(NSData *)data 替换为 -(IBAction)upload_selfie:(id)sender-(IBAction)upload_selfie :(UIButton*)button 因为你看起来很困惑。
您可以添加的内容:发送后将self.imageDataToSend 设置为nil。在 IBAction 方法的开头检查 imageDataToSend 是否不是 nil。我不知道你所有的代码是如何工作的,但是一旦你点击了 upload_selfie 你可能还想禁用该按钮,以防用户点击多次(并且你多次发送不必要的相同的图像),并在发送完成后“重新启用”(使用 [button:setEnabled:TRUE/FALSE];)。

关于ios - 上传图像以解析数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23113208/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com