我想从解析中删除照片。我怎么能这样做呢?它看起来很简单,但我做不到,并没有真正的资料来说明如何去做。这是我的保存代码。我正在保存一个名为Topc的课程。
- (void)uploadImage:(NSData *)imageData
{
PFFile *imageFile = [PFFile fileWithName:@"TopsImage.jpg" data:imageData];
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
// Set determinate mode
HUD.mode = MBProgressHUDModeDeterminate;
HUD.delegate = self;
HUD.labelText = @"Uploading";
[HUD show:YES];
// Save PFFile
[imageFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
//Hide determinate HUD
[HUD hide:YES];
// Show checkmark
HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
// The sample image is based on the work by http://www.pixelpressicons.com, http://creativecommons.org/licenses/by/2.5/ca/
// Make the customViews 37 by 37 pixels for best results (those are the bounds of the build-in progress indicators)
HUD.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
// Set custom view mode
HUD.mode = MBProgressHUDModeCustomView;
HUD.delegate = self;
// Create a PFObject around a PFFile and associate it with the current user
PFObject *userPhoto = [PFObject objectWithClassName:@"Topc"];
[userPhoto setObject:imageFile forKey:@"imageFile"];
// Set the access control list to current user for security purposes
userPhoto.ACL = [PFACL ACLWithUser:[PFUser currentUser]];
PFUser *user = [PFUser currentUser];
[userPhoto setObject:user forKey:@"user"];
[userPhoto saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (!error) {
[self refresh:nil];
}
else{
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];
}
else{
[HUD hide:YES];
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
} progressBlock:^(int percentDone) {
// Update your progress spinner here. percentDone will be between 0 and 100.
HUD.progress = (float)percentDone/100;
}];
}
答案 0 :(得分:1)
我在PFFile上写了一个允许你更新文件的类别,这意味着删除旧文件,然后上传新文件同名。我原本以为替换关联对象中的文件会删除旧文件,但它并没有因为我的数据使用量奇怪地增加。
+(void)updateFileWithName:(NSString*)name data:(NSData*)imageData completion:(void(^)(PFFile* file))completion
{
NSString* endpoint = [NSString stringWithFormat: @"https://api.parse.com/1/files/%@", name];
NSURL* url = [NSURL URLWithString:endpoint];
NSMutableURLRequest* req = [NSMutableURLRequest requestWithURL:url];
[req setHTTPMethod:@"DELETE"];
[req setValue:kParseApplicationKey forHTTPHeaderField:@"X-Parse-Application-Id"];
[req setValue:kParseMasterKey forHTTPHeaderField:@"X-Parse-Master-Key"];
NSLog(@"Updating file: %@", name);
[NSURLConnection sendAsynchronousRequest:req
queue:[NSOperationQueue currentQueue]
completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
NSLog(@"Deleted old file");
PFFile* file = [PFFile fileWithName:name data:imageData];
[file saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
NSLog(@"Saved new file");
completion(file);
}];
}];
}
您可以通过删除NSURLConnection
完成处理程序中的块
答案 1 :(得分:1)
[ParsePhotoPFObject deleteEventually];
答案 2 :(得分:0)
首先要删除图片或完整的行,您需要触发查询从Parse db中获取它,如下所示:
PFQuery *query = [PFQuery queryWithClassName:@"Topc"];
[query whereKeyExists:@"imageFile"]; //Change this condition to fetch the row.
[query getFirstObjectInBackgroundWithBlock:^(PFObject *object, NSError *error)
{
if (!error) {
NSLog(@"Successfully retrieved: %@", object);
//This might work as I searched for this deleting image but there is no method to do so.
//So a way would be is to get that object and then setting nil value in it's place.
//Else if u want to delete the entire row then u could simply type, [object deleteInBackground]
object[@"imageFile"] = [NSNull null];
}
else
{
NSLog(@"Error: %@", [error localizedDescription]);
}
}];
答案 3 :(得分:-2)
在您的代码中,您分两步添加图片:
您没有说出“删除”的含义,因此您有两个选择:
或
如果你想保留文件供以后使用,你只需在userPhoto类上调用deleteInBackground。
如果要删除两者,则应首先删除带有deleteInBackgroundWithBlock的文件,然后在块中,如果成功,则删除userPhoto:
PFFile *imageFile = userPhoto[@"imageFile"];
[imageFile deleteInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
if (succeeded) {
// Successfully deleted the image file. Now delete userPhoto:
[userPhoto deleteInBackground];
}
}];