-3

我有以下代码启动相机应用程序,用户可以选择“使用照片”。然而什么也没有发生。

if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera] == YES) {
        UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;

        [self presentModalViewController:imagePickerController animated:YES];
    } 

我怎样才能从中获取 UIimage 并将其保存到相册中。理想情况下,我想将其保存到文档目录并知道如何执行该部分。谢谢!

4

4 回答 4

3
        -(void) imagePickerController:(UIImagePickerController *)UIPicker didFinishPickingMediaWithInfo:(NSDictionary *) info
{

 UIImage* originalImage = nil;
    originalImage = [info objectForKey:UIImagePickerControllerEditedImage];
    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    if(originalImage==nil)
    {
        originalImage = [info objectForKey:UIImagePickerControllerCropRect];
    }



    [addImageOutlet setImage:originalImage forState:UIControlStateNormal];
       [self saveBackgroundImageInDocumentDirectory:originalImage];


    [self dismissViewControllerAnimated:YES completion:nil];

}
于 2013-10-30T12:11:16.433 回答
2

使用此代码:

 - (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    UIImage  *image =  [info objectForKey:UIImagePickerControllerOriginalImage];
    NSData * imageData = UIImagePNGRepresentation(image);

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"];
    [imageData writeToFile:savedImagePath atomically:NO];

    [picker dismissViewControllerAnimated:YES completion:nil];
}
于 2013-10-26T08:38:40.830 回答
1

我想对你有帮助

 //you have to set the delegate which clicking the button of camera

 imagePickerController.delegate = self;


//This Delegate method will call

- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{

    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage* selectedImage =  [info objectForKey:UIImagePickerControllerOriginalImage];

   NSString *path = [[self pathToPatientPhotoFolder] stringByAppendingPathComponent:[NSString stringWithFormat:@"imageName.png"]];

   NSError * error11 = nil;

   [post.getData writeToFile:path options:NSDataWritingAtomic error:&error11];

}



//This method is useful for the get the Documentry path

    - (NSString *)pathToPatientPhotoFolder {
        NSString *documentsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                                            NSUserDomainMask,
                                                                            YES) lastObject];
        NSString *myalbumpath = [documentsDirectory stringByAppendingPathComponent:@"MYAlbum"];

        // Create the folder if necessary
        BOOL isDir = NO;
        NSFileManager *fileManager = [[NSFileManager alloc] init];
        if (![fileManager fileExistsAtPath:myalbumpath
                               isDirectory:&isDir] && isDir == NO) {
            [fileManager createDirectoryAtPath:myalbumpath
                   withIntermediateDirectories:NO
                                    attributes:nil
                                         error:nil];
        }
        return myalbumpath;
    }

享受编码

于 2013-10-26T08:25:51.200 回答
1

addImageOutlet 用于什么类型的对象

于 2015-10-21T07:28:58.723 回答