IOS Parse Anypic无法正确保存图像

时间:2014-08-12 13:13:01

标签: objective-c parse-platform objective-c-blocks

我正在尝试使用AnyPic示例解析应用代码来创建类似于instagram的内容。我选择使用AVFoundation类来获得更多自定义的感觉,而不是使用UIImagePickerController中内置的AnyPics。在这一点上,我只是想保存我的图像,以下是发生这一切的功能:

首先使用图像初始化视图控制器:

- (id)initWithImage:(UIImage *)aImage {
    self = [super initWithNibName:nil bundle:nil];
    if (self) {
        if (!aImage) {
            return nil;
        }

        self.image = aImage;
        self.fileUploadBackgroundTaskId = UIBackgroundTaskInvalid;
        self.photoPostBackgroundTaskId = UIBackgroundTaskInvalid;
    }
    return self;
}

在视图中确实调用了loadUpload:

[self shouldUploadImage:self.image];

shouldUpload看起来像这样:

- (BOOL)shouldUploadImage:(UIImage *)anImage {
    NSLog(@"FTEditPhotoViewController::shouldUploadImage %@",anImage);
    UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit bounds:CGSizeMake(560.0f, 560.0f) interpolationQuality:kCGInterpolationHigh];
    UIImage *thumbnailImage = [anImage thumbnailImage:86.0f transparentBorder:0.0f cornerRadius:10.0f interpolationQuality:kCGInterpolationDefault];

    NSLog(@"shouldUploadImage::resizedImage: %@",resizedImage);
    NSLog(@"shouldUploadImage::thumbnailImage: %@",thumbnailImage);

    // JPEG to decrease file size and enable faster uploads & downloads
    NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.8f);
    NSData *thumbnailImageData = UIImagePNGRepresentation(thumbnailImage);

    //NSLog(@"shouldUploadImage::imageData: %@",imageData);
    //NSLog(@"shouldUploadImage::thumbnailImageData: %@",thumbnailImageData);

    if (!imageData || !thumbnailImageData) {
        return NO;
    }

    self.photoFile = [PFFile fileWithData:imageData];
    self.thumbnailFile = [PFFile fileWithData:thumbnailImageData];

    NSLog(@"shouldUploadImage::photoFile: %@",self.photoFile);
    NSLog(@"shouldUploadImage::thumbnailFile: %@",self.thumbnailFile);

    // 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];
    }];

    [self.photoFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [self.thumbnailFile saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                [[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];

                if (error) {
                    NSLog(@"self.thumbnailFile saveInBackgroundWithBlock: %@", error);
                }
            }];
        } else {
            [[UIApplication sharedApplication] endBackgroundTask:self.fileUploadBackgroundTaskId];
        }

        if (error) {
            NSLog(@"self.photoFile saveInBackgroundWithBlock: %@", error);
        }
    }];

    return YES;
}

最后,当用户点击发送/发布时,功能将在以下代码中处理:

- (void)doneButtonAction:(id)sender {
    NSLog(@"FTEditPhotoViewController::doneButtonAction");
    NSDictionary *userInfo = [NSDictionary dictionary];
    NSString *trimmedComment = [self.commentTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (trimmedComment.length != 0) {
        userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
                    trimmedComment,kFTEditPhotoViewControllerUserInfoCommentKey,
                    nil];
    }

    // Make sure there were no errors creating the image files
    if (!self.photoFile || !self.thumbnailFile) {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't post your photo" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
        [alert show];
        return;
    }

    // both files have finished uploading

    NSLog(@"doneButtonAction::self.photoFile: %@",self.photoFile);
    NSLog(@"doneButtonAction::self.thumbnailFile: %@",self.thumbnailFile);

    // create a photo object
    PFObject *photo = [PFObject objectWithClassName:kFTPhotoClassKey];
    [photo setObject:[PFUser currentUser] forKey:kFTPhotoUserKey];
    [photo setObject:self.photoFile forKey:kFTPhotoPictureKey];
    [photo setObject:self.thumbnailFile forKey:kFTPhotoThumbnailKey];

    NSLog(@"doneButtonAction::PFObject photo: %@",photo);

    // photos are public, but may only be modified by the user who uploaded them
    PFACL *photoACL = [PFACL ACLWithUser:[PFUser currentUser]];
    [photoACL setPublicReadAccess:YES];
    photo.ACL = photoACL;

    NSLog(@"doneButtonAction::PFObject photoACL: %@",photoACL);

    // Request a background execution task to allow us to finish uploading the photo even if the app is backgrounded
    self.photoPostBackgroundTaskId = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId];
        NSLog(@"FTEditPhotoViewController::doneButtonAction::saveInBackgroundWithBlock - photoPostBackgroundTaskId");
    }];

    // Save the Photo PFObject
    [photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {

            NSLog(@"FTEditPhotoViewController::doneButtonAction::saveInBackgroundWithBlock - succeeded");

            [[FTCache sharedCache] setAttributesForPhoto:photo likers:[NSArray array] commenters:[NSArray array] likedByCurrentUser:NO];

            // userInfo might contain any caption which might have been posted by the uploader
            if (userInfo) {
                NSString *commentText = [userInfo objectForKey:kFTEditPhotoViewControllerUserInfoCommentKey];

                if (commentText && commentText.length != 0) {
                    // create and save photo caption
                    PFObject *comment = [PFObject objectWithClassName:kFTActivityClassKey];
                    [comment setObject:kFTActivityTypeComment forKey:kFTActivityTypeKey];
                    [comment setObject:photo forKey:kFTActivityPhotoKey];
                    [comment setObject:[PFUser currentUser] forKey:kFTActivityFromUserKey];
                    [comment setObject:[PFUser currentUser] forKey:kFTActivityToUserKey];
                    [comment setObject:commentText forKey:kFTActivityContentKey];

                    PFACL *ACL = [PFACL ACLWithUser:[PFUser currentUser]];
                    [ACL setPublicReadAccess:YES];
                    comment.ACL = ACL;

                    [comment saveEventually];
                    [[FTCache sharedCache] incrementCommentCountForPhoto:photo];
                }
            } else {
                [photo saveEventually];
            }

            [[NSNotificationCenter defaultCenter] postNotificationName:FTTabBarControllerDidFinishEditingPhotoNotification object:photo];
        } else {
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Couldn't post your photo" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:@"Dismiss", nil];
            [alert show];
        }
        [[UIApplication sharedApplication] endBackgroundTask:self.photoPostBackgroundTaskId];
    }];

    // Dismiss this screen
    [self.parentViewController dismissViewControllerAnimated:YES completion:nil];
}

我已经跟踪了照片和缩略图,他们似乎确实在那里,但只是没有被保存到解析。

[5934:60b] FTEditPhotoViewController::shouldUploadImage <UIImage: 0x14d3bbf0>
[5934:60b] shouldUploadImage::resizedImage: <UIImage: 0x14db07e0>
[5934:60b] shouldUploadImage::thumbnailImage: <UIImage: 0x14db0c80>
[5934:60b] shouldUploadImage::photoFile: <PFFile: 0x14eb99d0>
[5934:60b] shouldUploadImage::thumbnailFile: <PFFile: 0x14db17d0>
[5934:60b] FTEditPhotoViewController::doneButtonAction
[5934:60b] doneButtonAction::self.photoFile: <PFFile: 0x14eb99d0>
[5934:60b] doneButtonAction::self.thumbnailFile: <PFFile: 0x14db17d0>
[5934:60b] doneButtonAction::PFObject photo: <Photo:new:(null)> {
    ACL = "<PFACL: 0x14db43e0>";
    image = "<PFFile: 0x14eb99d0>";
    thumbnail = "<PFFile: 0x14db17d0>";
    user = "<PFUser:WDk0diNx8m>";
}
[5934:60b] doneButtonAction::PFObject photoACL: <PFACL: 0x14da8180>
[5934:60b] FTEditPhotoViewController::doneButtonAction::saveInBackgroundWithBlock - succeeded

当我在解析我的班级时,我看到的是没有图像的单词文件。任何人都可以帮我弄清楚为什么图像没有保存解析?

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

Timothys建议打印出URL后,我意识到该URL没有文件扩展名。然后我注意到,每当我在解析时单击文件条目时,文件就会被下载到我的桌​​面,并且图像正在解析时保存。但是,它没有按照我的预期保存为缩略图的PNG或PNG。 URL以“-file”结尾,该文件是下载存储在parse中的文件的链接。我通过使用以下代码解决了我的问题:

self.photoFile = [PFFile fileWithName:@"photo.jpeg" data:imageData];
self.self.thumbnailFile = [PFFile fileWithName:@"thumbnail.png" data:imageData];

分别将图像保存为jpeg或缩略图。