运行Parse Anypic代码时崩溃。
-(void)sendCommentButton:(id) sender {
NSString *trimmedComment = [commentTextView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
if (trimmedComment.length != 0 && [self.photo objectForKey:kPAPPhotoUserKey]) {
PFObject *comment = [PFObject objectWithClassName:kPAPActivityClassKey];
[comment setObject:trimmedComment forKey:kPAPActivityContentKey]; // Set comment text
[comment setObject:[self.photo objectForKey:kPAPPhotoUserKey] forKey:kPAPActivityToUserKey]; // Set toUser
[comment setObject:[PFUser currentUser] forKey:kPAPActivityFromUserKey]; // Set fromUser
[comment setObject:kPAPActivityTypeComment forKey:kPAPActivityTypeKey];
[comment setObject:self.photo forKey:kPAPActivityPhotoKey];
[comment setObject:self.photoFile forKey:@"attachmentFile"];
PFACL *ACL = [PFACL ACLWithUser:[PFUser currentUser]];
[ACL setPublicReadAccess:YES];
[ACL setWriteAccess:YES forUser:[self.photo objectForKey:kPAPPhotoUserKey]];
comment.ACL = ACL;
[[PAPCache sharedCache] incrementCommentCountForPhoto:self.photo];
// Show HUD view
[MBProgressHUD showHUDAddedTo:self.view.superview animated:YES];
// If more than 5 seconds pass since we post a comment, stop waiting for the server to respond
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(handleCommentTimeout:) userInfo:@{@"comment": comment} repeats:NO];
[comment saveEventually:^(BOOL succeeded, NSError *error) {
[timer invalidate];
if (error && error.code == kPFErrorObjectNotFound) {
[[PAPCache sharedCache] decrementCommentCountForPhoto:self.photo];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"Could not post comment", nil) message:NSLocalizedString(@"This photo is no longer available", nil) delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil];
[alert show];
[self.navigationController popViewControllerAnimated:YES];
}
[[NSNotificationCenter defaultCenter] postNotificationName:PAPPhotoDetailsViewControllerUserCommentedOnPhotoNotification object:self.photo userInfo:@{@"comments": @(self.objects.count + 1)}];
[MBProgressHUD hideHUDForView:self.view.superview animated:YES];
[self loadObjects];
}];
}
[self.commentTextView setText:@""];
[self.commentTextView resignFirstResponder];
if (self.photoFile != nil) {
self.photoFile = nil;
}
}
选择图片
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
NSLog(@"Hello");
UIImage *img = [info objectForKey:UIImagePickerControllerOriginalImage];
// JPEG to decrease file size and enable faster uploads & downloads
NSData *imageData = UIImageJPEGRepresentation(img, 0.8f);
self.photoFile = [PFFile fileWithData:imageData];
// Request a background execution task to allow us to finish uploading the photo even if the app is backgrounded
self.fileUploadBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}];
NSLog(@"Requested background expiration task with id %lu for Anypic photo upload", (unsigned long)self.fileUploadBackgroundTaskId);
[self.photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
NSLog(@"Photo uploaded successfully");
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Photo Uploaded"
message:@"successfully"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
} else {
[[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
}
}];
[self dismissViewControllerAnimated:YES completion:nil];
}
问题:为什么会崩溃?我相信这是崩溃它的代码。我做了什么来崩溃它不是添加attachmentFile只是发表评论。
如果您需要更多代码或需要任何说明,请在下面发表评论
答案 0 :(得分:0)
PFObject *comment = [PFObject objectWithClassName:kPAPActivityClassKey];
[comment setObject:trimmedComment forKey:kPAPActivityContentKey]; // Set comment text
[comment setObject:[self.photo objectForKey:kPAPPhotoUserKey] forKey:kPAPActivityToUserKey]; // Set toUser
[comment setObject:[PFUser currentUser] forKey:kPAPActivityFromUserKey]; // Set fromUser
[comment setObject:kPAPActivityTypeComment forKey:kPAPActivityTypeKey];
[comment setObject:self.photo forKey:kPAPActivityPhotoKey];
[comment setObject:self.photoFile forKey:@"attachmentFile"];
在其中一行中,您将nil传递给setObject:forKey的第一个参数。添加一个异常断点(在左侧边栏的断点选项卡中)并检查它断行的那一行。