2011-09-30 36 views
12

Tôi đang làm việc trên một ứng dụng gần như giống với mã mẫu tôi tìm thấy ở đây. Nhưng phương pháp tôi muốn làm là khác với các mã mẫu.
Liên kết: PhotoLocationsCách tải ảnh từ thư viện ảnh và lưu trữ nó vào dự án ứng dụng?

Màn hình đầu tiên sẽ có nút ImageView và 2 (Chọn ảnh, xác nhận). Khi nút 'Chọn ảnh' được nhấn, nó sẽ điều hướng đến màn hình khác để truy xuất ảnh từ thư viện ảnh của iPad. Khi một bức ảnh được chọn, nó sẽ loại bỏ màn hình hiện tại và trở về màn hình đầu tiên hiển thị ảnh đã chọn trên ImageView.

Khi nút 'Xác nhận' được khai thác, ảnh sẽ được lưu trữ trong dự án của ứng dụng của tôi (ví dụ: /resources/images/photo.jpg).

Tôi có thể biết cách tôi có thể thực hiện việc này không?

+0

trong iPhone bạn phải sử dụng UIImagePickerController để lấy hình ảnh từ thư viện và bạn có thể có chúng vào ứng dụng ur bằng cách tiết kiệm nó trong NSDocumentsDirectory – booleanBoy

+0

Có cảm ơn bạn, nhưng tôi muốn có một số mã mẫu để mà tôi có thể tham khảo. – Lloydworth

+2

google "Tham chiếu UIImagePickerController". Truy cập trang web của Apple. – Akshay

Trả lời

32

này sẽ đưa bạn đến thư viện hình ảnh và bạn có thể chọn hình ảnh.

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

này sẽ giúp bạn chọn hình ảnh

- (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; 
} 

Và sau đó bạn có thể lưu trữ hình ảnh này vào thư mục tài liệu ...

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

Đối với cách nhấn vào hình ảnh bản thân sử dụng

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

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

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

Tôi có thể biết cách lưu hình ảnh từ ImageView vào thư mục tài liệu không? – Lloydworth

+2

Chỉ cần chỉnh sửa câu trả lời của tôi ở trên ... –

+0

Tôi chỉ nghĩ về một chức năng bổ sung. Trong màn hình đầu tiên, tôi sẽ thay đổi nút 'Chọn Ảnh' thành 'Duyệt từ thư viện ảnh' và cũng thêm nút mới 'Chụp ảnh mới'. Bằng cách nhấn vào nút đó, ứng dụng của tôi sẽ khởi chạy máy ảnh và khi người dùng chụp ảnh, nó sẽ trở lại màn hình đầu tiên hiển thị hình ảnh trên ImageView có thể được lưu vào thư mục tài liệu của tôi. – Lloydworth

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

Và phương pháp đại biểu này sẽ trở lại với hình ảnh u chọn từ album

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

trong đó noImage là UIImageView trong đó tôi đang hiển thị hình ảnh được chọn từ album ... – booleanBoy

+0

Tôi có thể biết làm cách nào để lưu trữ noImage vào thư mục tài liệu không? – 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

Đây là câu trả lời mới cho câu hỏi cũ với câu trả lời đã được chấp nhận. Câu trả lời của bạn cho biết câu trả lời được chấp nhận là gì? Ngoài ra, thêm giải thích về cách mã này hoạt động sẽ giúp khách truy cập trong tương lai. – JAL

Các vấn đề liên quan