创建我自己的iphone Photolibrary

时间:2012-03-12 12:02:02

标签: ios iphone objective-c ios5

我想制作我自己的照片库,从我的图书馆获取图像并将其保存到我的图书馆并询问是否需要更改...是否会感谢任何帮助

问候

·高斯

3 个答案:

答案 0 :(得分:5)

你可以让用户使用UIImagePickerControl选择要导入的图像。有关详细信息,请参阅此优秀帖 https://stackoverflow.com/questions/1282830/uiimagepickercontroller-uiimage-memory-and-more

然后使用类似的内容将图像保存/加载到您自己的应用程序

- (void)saveImage: (UIImage*)image
{
    if (image != nil)
    {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                      NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                       [NSString stringWithString: @"test.png"] ];
        NSData* data = UIImagePNGRepresentation(image);
        [data writeToFile:path atomically:YES];
    }
}



- (UIImage*)loadImage
{
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 
                      NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString* path = [documentsDirectory stringByAppendingPathComponent: 
                      [NSString stringWithString: @"test.png"] ];
    UIImage* image = [UIImage imageWithContentsOfFile:path];
    return image;
}

答案 1 :(得分:-2)

添加:

-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker
{


    if ([self isPad]) {

        if(picker.sourceType==UIImagePickerControllerSourceTypePhotoLibrary)
        {

            [popoverController dismissPopoverAnimated:YES];

        }
        else
        {
            [picker  dismissModalViewControllerAnimated:YES];
            [picker release];

        }

    }   
    else
    {

        [picker dismissModalViewControllerAnimated:YES]; 
    }

}

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

{   
    UIImage *imageToScale=[info objectForKey:UIImagePickerControllerOriginalImage];

    flagImageSelected=YES;
    /*Set Image on the Btn with Image Insets.*/    
    //Image Insets TOP, LEFT, BOTTOM, RIGHT
    if([self isPad])
    {
        [btnImage setImageEdgeInsets:UIEdgeInsetsMake(31.0,33.0,36,36.0)];

        //imageToScale =[imageToScale scale:CGSizeMake(530, 546)];
        imageToScale=[imageToScale scaleToSize:CGSizeMake(530, 546)];  


    }
    else
    {
        [btnImage setImageEdgeInsets:UIEdgeInsetsMake(13.0,13.0,14.0,14.0)];
        imageToScale=[imageToScale scaleToSize:CGSizeMake(300, 300)];  

    }

    [btnImage setImage:imageToScale forState:UIControlStateNormal]; 



    if ([self isPad]) {

        if(picker.sourceType==UIImagePickerControllerSourceTypePhotoLibrary)
        {

            [popoverController dismissPopoverAnimated:YES];

        }
        else
        {
            [picker  dismissModalViewControllerAnimated:YES];
            [picker release];

        }

    }   
    else
    {
        [picker dismissModalViewControllerAnimated:YES];
        //[picker release];
    }
}
-(NSString *) getImagePath{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES); 
    NSString *documentsDirectory = [paths objectAtIndex:0];

    NSError *error;
    [[NSFileManager defaultManager] createDirectoryAtPath:documentsDirectory withIntermediateDirectories:YES attributes:nil error:&error]; 
    return documentsDirectory;
}

答案 2 :(得分:-4)

-(IBAction)buttonClicked:(id)sender
{
    UIActionSheet *action = [[UIActionSheet alloc] initWithTitle:@"Select and capture photos in LANDSCAPE mode for best results" delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Photo Library",@"Capture", nil];
    [action showInView:self.view];
    [action release];
}

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0)
    {
        if ([self isPad])
        {    
            UIImagePickerController* picker = [[UIImagePickerController alloc] init]; 
            picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
            picker.delegate = self;    
            NSLog(@"POP OVER CONTROLLER Retain Count Before %d",[popoverController retainCount]);

            if (popoverController) popoverController = nil;
            popoverController = [[UIPopoverController alloc] initWithContentViewController:picker];

            [popoverController presentPopoverFromRect:CGRectMake(btnImage.frame.origin.x, btnImage.frame.origin.y, btnImage.frame.size.width, btnImage.frame.size.height) inView:self.view permittedArrowDirections:UIPopoverArrowDirectionRight animated:YES];
            NSLog(@"POP OVER CONTROLLER Retain Count Before %d",[popoverController retainCount]);

            [picker release];
        }
        else 
        {
            if(ipc)
            {
                ipc=nil;
                [ipc release];
            }
            ipc=[[UIImagePickerController alloc] init];
            ipc.delegate=self;

            ipc.sourceType=UIImagePickerControllerSourceTypePhotoLibrary;
            [self presentModalViewController:ipc animated:YES];
        }        
    }
    else if (buttonIndex == 1)
    {
        if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
        {
            if(ipc)
            {
                [ipc release];
                ipc=nil;
            }
            ipc=[[UIImagePickerController alloc] init];
            ipc.delegate=self;
            ipc.sourceType=UIImagePickerControllerSourceTypeCamera;

            [self presentModalViewController:ipc animated:YES];
        }
        else
        {  
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"winEaux" message:@"Camera capture is not supported in this device" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
            [alert show];
            [alert release];
        }
    }
}