我是一般的编码新手,并根据苹果和解析代码构建了一个照片部分应用。
尽管有许多尝试调整代码,但我无法将图像保存到我的解析仪表板中。你能告诉我哪里出错了吗?
我使用的代码是:
- (IBAction)showImagePickerForPhotoPicker:(id)sender
{
NSLog(@"Code shows image picker for album in APLViewController.m");
[self showImagePickerForSourceType:UIImagePickerControllerSourceTypePhotoLibrary];
...
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
NSString *mediaType = [info objectForKey: UIImagePickerControllerMediaType];
UIImage *originalImage, *editedImage, *imageToUse;
// Handle a still image picked from a photo album
if (CFStringCompare ((CFStringRef) mediaType, kUTTypeImage, 0)
== kCFCompareEqualTo) {
editedImage = (UIImage *) [info objectForKey:
UIImagePickerControllerEditedImage];
originalImage = (UIImage *) [info objectForKey:
UIImagePickerControllerOriginalImage];
if (editedImage) {
imageToUse = editedImage;
} else {
imageToUse = originalImage;
}
UIImage *image = [info valueForKey:UIImagePickerControllerOriginalImage];
NSLog(@"Code reached parse save code");
NSData *imageData = UIImagePNGRepresentation (originalImage);
PFFile *imageFile = [PFFile fileWithName:@"image.png" data:imageData];
PFObject *userPhoto = [PFObject objectWithClassName:@"UserPhoto"];
userPhoto[@"imageName"] = @"My trip to Hawaii!";
userPhoto[@"imageFile"] = imageFile;
[userPhoto saveInBackground];
NSLog(@"code reaches addobject to captured images array");
[self.capturedImages addObject:image];
UIGraphicsBeginImageContext(CGSizeMake(640, 960));
[image drawInRect: CGRectMake(0, 0, 640, 960)];
由于
答案 0 :(得分:0)
我认为保存过程需要在imagePicker之外。看起来应该是这样的。并且当所有事情都在周围时,应该运行完成图像。
PFUser *profile = [PFUser currentUser];
NSData *imageData = UIImageJPEGRepresentation(profileImage.image, 0.8);
NSString *filename = [NSString stringWithFormat:@"%@", profile.username];
PFFile *imageFile = [PFFile fileWithName:filename data:imageData];
[profile setObject:imageFile forKey:@"profileimageFile"];
[profile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
// Show success message
}
else {
// Show error message
}
}];
要保存个人资料图片,您需要先选择以保存图像。在这种情况下是在currentUser。然后将您选择的图像转换为NSData文件。然后,您需要为该数据文件命名。然后你创建一个PFFile 。然后保存该文件进行解析。
获取图片的方式与此相同但反向:
PFFile *userImageFile = [PFUser currentUser][@"profileImageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
if (!error) {
UIImage *image = [UIImage imageWithData:imageData];
}
}];