2011-10-19 36 views
7

Trong ứng dụng của tôi, tôi muốn người dùng có thể chụp ảnh hoặc sử dụng ảnh từ thư viện ảnh. Khi người dùng nhấp vào nút tôi đã thực hiện chế độ xem cảnh báo bật lên, người dùng có thể chọn giữa chụp ảnh mới hoặc ảnh từ thư viện ảnh. Đây là mã tôi đã sử dụng:Tại sao giao diện máy ảnh của tôi hoạt động lạ khi tôi sử dụng UIImagePickerController?

- (void)PictureAlert:(id)sender { 

    UIAlertView *AlertDialog; 

    // Setting up AlertDialog. 
    AlertDialog = [[UIAlertView alloc] initWithTitle:nil 
              message:nil 
              delegate:self 
            cancelButtonTitle:@"Cancel" 
            otherButtonTitles:@"Choose From Library", @"Take New Picture", nil]; 

    [AlertDialog show]; } 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex { 

    NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex]; 

    if ([ButtonTitle isEqualToString:@"Choose From Library"]) { 

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 

      // Pick photo. 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
      picker.delegate = self; 
      picker.allowsEditing = YES; 
      picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 

      [self presentModalViewController:picker animated:YES]; 


     } else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) { 

      // Setting up AlertDialog. 
      UIAlertView *AlertDialog; 

      AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing photo library" 
                message:@"Device does not support a photo library" 
                delegate:self 
              cancelButtonTitle:@"Dismiss" 
              otherButtonTitles:nil]; 

      [AlertDialog show]; 

     } 


    } else if ([ButtonTitle isEqualToString:@"Take New Picture"]) { 

     if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 

      // Take new photo. 
      UIImagePickerController *picker = [[UIImagePickerController alloc] init]; 
      picker.delegate = self; 
      picker.allowsEditing = YES; 
      picker.wantsFullScreenLayout = YES; 
      picker.sourceType = UIImagePickerControllerSourceTypeCamera; 

      [self presentModalViewController:picker animated:YES]; 


     } else if (![UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 

      // Setting up AlertDialog. 
      UIAlertView *AlertDialog; 

      AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing camera" 
                message:@"Device does not support a camera" 
                delegate:self 
              cancelButtonTitle:@"Dismiss" 
              otherButtonTitles:nil]; 

      [AlertDialog show]; 

     } 

    } 

} 

Vấn đề là nếu người dùng muốn chụp một bức ảnh mới giao diện camera bật lên, và sau đó nếu bạn xoay thiết bị giao diện trông như thế này: enter image description here

Và sau đó khi người dùng xoay nó lại nó đột nhiên trông như thế này: enter image description here

một vấn đề bên ít là máy ảnh mất nhiều thời gian để tải.

Bất kỳ suy nghĩ sẽ được đánh giá :)

+0

tôi đang gặp phải vấn đề này. – nicholjs

Trả lời

0

Một số thời gian của mình đã xảy ra nếu bạn đang sử dụng một iphone thế hệ cũ mà có os hiện tại ví dụ bạn đang gặp iphone 3G và bạn cập nhật ios của mình cho iOS5 sau đó một số các ứng dụng bạn đã cài đặt có thể hoạt động khác nhau, bạn có thể kiểm tra ứng dụng của mình với một thiết bị khác để khắc phục sự cố của bạn.

+0

Nhưng tôi có một chiếc iPhone 4 .... vì vậy nó vẫn còn là một vấn đề? –

+0

ok, sau đó thử hướng dẫn này do apple cung cấp để thực hiện các thao tác trên máy ảnh của bạn. http://stackoverflow.com/questions/7763459/save-the-captured-image-with-the-overlay-image-to-the-photoalbum-in-iphone/7763483#7763483 – Ballu

0

Đảm bảo bạn đặt cấu trúc phân cấp bộ điều khiển chế độ xem, với mainWindow.rootViewController và [vc addChildViewController:]. Điều này lan truyền thông tin định hướng xuống đến nơi bạn cần.

+0

có vẻ như anh ấy đang làm điều này trong mã mà anh ta đã đăng với currentModelViewController. – nicholjs

1

Một số điều bạn có thể muốn xem xét:

  1. Thiết lập wantsFullScreenLayout tài sản để YES sẽ gây ra quan điểm để bỏ qua thanh trạng thái. Nhưng vì bạn đang sử dụng các điều khiển camera mặc định, thanh trạng thái sẽ tự động ẩn. Đây là nguyên nhân có khả năng nhất cho vùng màu xám 20 pixel ở cuối hình ảnh.

  2. Điều khiển camera mặc định được thiết kế ở chế độ dọc. Kể từ khi hình ảnh đầu tiên của bạn trông giống như bạn bằng cách nào đó xoay màn hình, bạn nên nhìn vào mã của bạn (có lẽ là shouldAutoRotate) và xem lý do tại sao bạn đang quay chế độ xem như thế. Điều này sẽ khắc phục được sự cố về mức thu phóng bạn đang nhận được trong ảnh phong cảnh của mình.

  3. Bạn sẽ bị rò rỉ bộ nhớ nếu bạn tạo UIImagePickerController, trình bày nó, và sau đó không có tham chiếu đến nó để phát hành sau này. Tôi sẽ khuyên bạn nên thiết lập các UIImagePickerController trong giao diện, và thiết lập nó trong phương pháp viewDidLoad. Hãy thử:

.h

@interface yourView:UIViewController <UIImagePickerControllerDelegate> { 
    UIImagePickerController * picker; 
} 

.m

- (void)dealloc; { 
    [picker release]; 
    [super dealloc]; 
} 

- (void)viewDidLoad; { 
    [super viewDidLoad]; 
    picker = [[UIImagePickerController alloc] init]; 
    picker.delegate = self; 
    picker.allowsEditing = YES; 
    picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
} 

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; { 
    NSString *ButtonTitle = [alertView buttonTitleAtIndex:buttonIndex]; 

    if([ButtonTitle isEqualToString:@"Choose From Library"]){ 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]){ 
     picker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum; 
     [self presentModalViewController:picker animated:YES]; 
    } 
    else{ 
     // Setting up AlertDialog. 
     UIAlertView *AlertDialog; 
     AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing camera" 
               message:@"Device does not support a camera" 
               delegate:self 
            cancelButtonTitle:@"Dismiss" 
            otherButtonTitles:nil]; 
     [AlertDialog show]; 
     [AlertDialog release]; 
    } 
    } 
    else if([ButtonTitle isEqualToString:@"Take New Picture"]){ 
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) { 
     picker.sourceType = UIImagePickerControllerSourceTypeCamera; 
     [self presentModalViewController:picker animated:YES]; 
    } 
    else{ 
     // Setting up AlertDialog. 
     UIAlertView *AlertDialog; 
     AlertDialog = [[UIAlertView alloc] initWithTitle:@"Error accessing camera" 
               message:@"Device does not support a camera" 
               delegate:self 
            cancelButtonTitle:@"Dismiss" 
            otherButtonTitles:nil]; 
     [AlertDialog show]; 
     [AlertDialog release]; 
    } 
    } 
} 

này nên làm sạch-up các rò rỉ bộ nhớ, và cải thiện thời gian tải. Hy vọng rằng Giúp!

+0

thật không may, điều này không khắc phục được sự cố xoay vòng. nhưng cảm ơn cho đầu bộ nhớ! – nicholjs

+0

Bạn có thể đăng thêm mã không? Nó phải là một cái gì đó trong bộ điều khiển xem của bạn, vì không có gì sai với mã trên. – msgambel

0

Dường như dự án của tôi đã xảy ra vì bạn chưa viết phương thức shouldAutoRotateToInterface: trong trình điều khiển chế độ xem gốc của bạn. Thông điệp luân phiên lan truyền tất cả các con đường xuống đến đại diện shouldAutoRotateToInterface của trình điều khiển xem gốc khi UIImagePickerController được gọi.Phương pháp của bạn sẽ trông giống như sau:

- (BOOL)shouldAutorotateToInterfaceOrientation(UIInterfaceOrientation)interfaceOrientation 
{ 
    // Return YES for supported orientations 
    return (interfaceOrientation == UIInterfaceOrientationPortrait); 
} 

Khi tôi nâng cấp dự án lên iOS 5, tôi đã mượn bộ điều khiển chế độ xem gốc từ dự án iOS 3. iOS 3 không tự động viết phương thức này trong các lớp điều khiển xem Hãy thử nó và cho tôi biết.

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