0

我使用 Anypic 开源将使用该应用拍摄的照片保存到应用内供稿。我想将其转换为保存视频而不是照片。我将如何去做这件事。这是我用来保存照片的代码-

- (BOOL)shouldUploadImage:(UIImage *)anImage {
// Resize the image to be square (what is shown in the preview)
UIImage *resizedImage = [anImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit
                                                      bounds:CGSizeMake(560.0f, 560.0f)
                                        interpolationQuality:kCGInterpolationHigh];
// Create a thumbnail and add a corner radius for use in table views
UIImage *thumbnailImage = [anImage thumbnailImage:86.0f
                                transparentBorder:0.0f
                                     cornerRadius:10.0f
                             interpolationQuality:kCGInterpolationDefault];

// Get an NSData representation of our images. We use JPEG for the larger image
// for better compression and PNG for the thumbnail to keep the corner radius transparency
NSData *imageData = UIImageJPEGRepresentation(resizedImage, 0.8f);
NSData *thumbnailImageData = UIImageJPEGRepresentation(thumbnailImage, 0.8f);

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

// Create the PFFiles and store them in properties since we'll need them later
self.photoFile = [PFFile fileWithData:imageData];
self.thumbnailFile = [PFFile fileWithData:thumbnailImageData];

// Save the files
[self.photoFile saveInBackground];
[self.thumbnailFile saveInBackground];

}

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

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

// create a photo object
PFObject *photo = [PFObject objectWithClassName:kPAPPhotoClassKey];
[photo setObject:[PFUser currentUser] forKey:kPAPPhotoUserKey];
[photo setObject:self.photoFile forKey:kPAPPhotoPictureKey];
[photo setObject:self.thumbnailFile forKey:kPAPPhotoThumbnailKey];

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

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

// save
[photo saveInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
    if (succeeded) {
        NSLog(@"Photo uploaded");

        [[PAPCache 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:kPAPEditPhotoViewControllerUserInfoCommentKey];

            if (commentText && commentText.length != 0) {
                // create and save photo caption
                PFObject *comment = [PFObject objectWithClassName:kPAPActivityClassKey];
                [comment setObject:kPAPActivityTypeComment forKey:kPAPActivityTypeKey];
                [comment setObject:photo forKey:kPAPActivityPhotoKey];
                [comment setObject:[PFUser currentUser] forKey:kPAPActivityFromUserKey];
                [comment setObject:[PFUser currentUser] forKey:kPAPActivityToUserKey];
                [comment setObject:commentText forKey:kPAPActivityContentKey];

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

                [comment saveEventually];
                [[PAPCache sharedCache] incrementCommentCountForPhoto:photo];
            }
        }

        [[NSNotificationCenter defaultCenter] postNotificationName:PAPTabBarControllerDidFinishEditingPhotoNotification object:photo];
    } else {
        NSLog(@"Photo failed to save: %@", error);
        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];
}];

[self.parentViewController dismissModalViewControllerAnimated:YES];

}

4

1 回答 1

0
PFFile *videoFile = [PFFile fileWithData:videoData];

[self.videoFile saveInBackground];

您会将视频视为解析中的任何其他文件。我会查看 Apple 的有关在 iOS 上处理视频文件的视频文档。

于 2014-05-04T18:19:44.367 回答