[英]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: 像这样在分析中制作了
selfie1
地方:
But I am getting an error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UIButton length]: unrecognized selector sent to instance 但是我遇到一个错误: 由于未捕获的异常'NSInvalidArgumentException',终止了应用程序,原因:'-[UIButton length]:无法识别的选择器发送到实例
Any ideas why? 有什么想法吗?
The break point notified me of this: 断点通知我:
And these: 还有这些:
And I have noticed this: 我注意到了这一点:
And this from the debugging: 而这来自调试:
upload_selfie
to the button upload_selfie
连接到按钮 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];
用
[self upload_selfie:nil withData:imageData];
调用它[self upload_selfie:nil withData:imageData];
The question: Why? 问题:为什么?
If you call an IBAction
method, you'll send with it the sender
. 如果调用
IBAction
方法,则将与它一起sender
。
Remember, we usually write: -(IBAction)actionMethod:(id)sender
. 记住,我们通常写
-(IBAction)actionMethod:(id)sender
。 If you want to check the sender, you may do, in case of a UIButton: UIButton *button = (UIButton *)sender;
如果要检查发件人,可以使用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]
. 因此,据我了解,如果通过用户交互(通过使用
[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. 但是我不明白的是为什么您一开始将它作为
IBAction
来使用,却似乎没有链接到“直接用户操作”。
Edit: 编辑:
After more explanation, I'd suggest this: Add: 经过更多的解释,我建议这样做:添加:
@property (nonatomic, strong) NSData *imageDataToSend;
Replace [self upload_selfie:imageData];
替换
[self upload_selfie:imageData];
with: [self setImageDataToSend:imageData];
与:
[self setImageDataToSend:imageData];
Replace PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:data];
替换
PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:data];
with: PFFile *imageFile = [PFFile fileWithName:@"Image.png" data:imageDataToSend];
其中:
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. 用
-(IBAction)upload_selfie:(id)sender
或-(IBAction)upload_selfie:(UIButton*)button
替换-(IBAction)upload_selfie:(NSData *)data
,因为您似乎很困惑。
What you may add: put self.imageDataToSend
to nil
once it's send. 您可能要添加的内容:发送后,将
self.imageDataToSend
为nil
。 Check at the beginning of the the IBAction
method if imageDataToSend
is not nil
. 检查
IBAction
方法的开头是否imageDataToSend
不为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];
). 我不知道您所有的代码如何工作,但是一旦您单击了
upload_selfie
您可能还希望禁用该按钮,以防用户多次单击(并且您发送了许多不必要的相同图像),并且“发送完成后(使用[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. 您的错误消息指出,向UIButton类型的对象发送的消息是“长度”,而没有该消息。 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. 您已经将imageFile设置为图像。 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) {
}];
})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.