2011-09-30 103 views
12

我正在处理与我在此处找到的示例代码几乎相同的应用程序。但是我想要做的方法与示例代码不同。
链接:PhotoLocations如何从照片库加载照片并将其存储到应用程序项目中?

第一个屏幕将有一个ImageView和2个按钮(选择照片,确认)。 当点击“选择照片”按钮时,它将导航到另一个屏幕,从iPad的照片库中检索照片。当选择照片时,它将关闭当前屏幕并返回到在ImageView上显示所选照片的​​第一个屏幕。

点击'确认'按钮后,照片将被存储到我的应用程序的项目中(例如/resources/images/photo.jpg)。

我可以知道我该怎么做?

+0

在iPhone,你必须使用的UIImagePickerController来检索图库图片,您可以通过它保存在NSDocumentsDirectory – booleanBoy

+0

是谢谢你让他们到乌尔应用程序,但我希望有一些示例代码,以便我可以参考。 – Lloydworth

+2

谷歌“UIImagePickerController参考”。转到Apple的网站。 – Akshay

回答

32

这将带你到图片库,你可以选择图像。

UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init]; 
imagePickerController.delegate = self; 
imagePickerController.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; 
[self presentViewController:imagePickerController animated:YES completion:nil]; 

这将帮助你选择图像

- (void)imagePickerController:(UIImagePickerController *)picker 
    didFinishPickingImage:(UIImage *)image 
       editingInfo:(NSDictionary *)editingInfo 
{ 
    // Dismiss the image selection, hide the picker and 

    //show the image view with the picked image 

    [picker dismissViewControllerAnimated:YES completion:nil]; 
    //UIImage *newImage = image; 
} 

然后你就可以保存此图片的文件目录...

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *savedImagePath = [documentsDirectory stringByAppendingPathComponent:@"savedImage.png"]; 
UIImage *image = imageView.image; // imageView is my image from camera 
NSData *imageData = UIImagePNGRepresentation(image); 
[imageData writeToFile:savedImagePath atomically:NO]; 

对于点击图片自己使用这个

- (IBAction) takePhoto:(id) sender 
{ 
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc] init]; 

    imagePickerController.delegate = self; 
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera; 

    [self presentModalViewController:imagePickerController animated:YES]; 
} 
+0

我可以知道如何将图像从ImageView存储到文档目录? – Lloydworth

+2

刚刚编辑我上面的答案... –

+0

我只是想到了一个额外的功能。在第一个屏幕中,我会将“选择照片”按钮更改为“从照片库浏览”,并添加一个新按钮“拍摄新照片”。通过点击该按钮,我的应用程序将启动相机,一旦用户拍摄照片,它将返回到第一个屏幕,显示ImageView上的图片,该图片也可以保存到我的文档目录中。 – Lloydworth

3
UIImagePickerController *cardPicker = [[UIImagePickerController alloc]init]; 
cardPicker.allowsEditing=YES; 
cardPicker.delegate=self; 
cardPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
[self presentModalViewController:appDelegate.cardPicker animated:YES]; 
[cardPicker release]; 

而且此委托方法将返回U选择将图像从专辑

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)img editingInfo:(NSDictionary *)editInfo 
{ 
    [[picker parentViewController] dismissModalViewControllerAnimated:YES]; 
    noImage.image=img; 
} 
+0

其中noImage是UIImageView,我在其中显示从相册中选取的图像... – booleanBoy

+0

我可以知道如何将noImage存储到文档目录吗? – Lloydworth

0

- (IBAction)UploadPhoto:(id)sender { 
 
    
 
    UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Please Select Your Option"delegate:self cancelButtonTitle:@"Cancel" destructiveButtonTitle:nil otherButtonTitles:@"Camera", @"Gallery",nil]; 
 
    
 
    [actionSheet showInView:self.view]; 
 
    
 
    UITableViewCell *clickedCell = (UITableViewCell *)[[sender superview] superview]; 
 
    
 
    NSIndexPath *clickedButtonPath = [jobstable indexPathForCell:clickedCell]; 
 
    
 
    isSectionIndex = clickedButtonPath.section; 
 
    isRowIndex  = clickedButtonPath.row; 
 
    
 
} 
 

 

 
-(void)actionSheet:(UIActionSheet *)actionSheet didDismissWithButtonIndex:(NSInteger)buttonIndex{ 
 
    
 
    
 
    NSLog(@"From didDismissWithButtonIndex - Selected Option: %@", [actionSheet buttonTitleAtIndex:buttonIndex]); 
 
    
 
    NSString*image=[actionSheet buttonTitleAtIndex:buttonIndex]; 
 
    
 
    if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Camera"]) { 
 
     
 
     if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) 
 
     { 
 
      UIAlertView* alert = [[UIAlertView alloc] initWithTitle:nil message:@"Device Camera Is Not Working" delegate:nil cancelButtonTitle:nil otherButtonTitles:@"OK", nil]; 
 
      [alert show]; 
 
      return; 
 
     } 
 
     else{ 
 
      
 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
 
      picker.delegate = self; 
 
      picker.allowsEditing = YES; 
 
      picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
 
      
 
      [self presentViewController:picker animated:YES completion:NULL]; 
 
      
 
     } 
 
    } 
 
    
 
    else if ([[actionSheet buttonTitleAtIndex:buttonIndex] isEqualToString:@"Gallery"]){ 
 
     
 
     
 
     UIImagePickerController *pickerView = [[UIImagePickerController alloc] init]; 
 
     pickerView.allowsEditing = YES; 
 
     pickerView.delegate = self; 
 
     [pickerView setSourceType:UIImagePickerControllerSourceTypeSavedPhotosAlbum]; 
 
     [self presentViewController:pickerView animated:YES completion:nil]; 
 
     
 
    } 
 
    
 
} 
 

 
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{ 
 
UIImage* orginalImage = [info objectForKey:UIImagePickerControllerOriginalImage]; 
 
     
 
     
 
     
 
     NSIndexPath *indexPath = [NSIndexPath indexPathForRow:isRowIndex inSection:isSectionIndex]; 
 
     
 
     UITableViewCell *cell = [jobstable cellForRowAtIndexPath:indexPath]; 
 
     
 
     UIImageView *tableIMAGE=(UIImageView *)[cell.contentView viewWithTag:19]; 
 
     
 
     tableIMAGE.image=orginalImage; 
 
     
 
    answersARRAY[indexPath.row] = [NSString stringWithFormat:@"-1,%@,%@,",answersARRAY[indexPath.row],imageStris]; 
 

 
    
 
     
 
     [self dismissViewControllerAnimated:YES completion:nil]; 
 
     
 
     
 
    } 
 
    
 
    - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker { 
 
     
 
     [picker dismissViewControllerAnimated:YES completion:NULL]; 
 
    
 
    }

+0

这是对已经接受答案的旧问题的新答案。你的回答提供了什么,接受的答案不是?此外,添加关于此代码如何工作的解释将有助于未来的访问者。 – JAL

相关问题