iOS-UIActionSheet的簡單使用和調用系統相簿

來源:互聯網
上載者:User

實現UIActionSheetDelegate

UIActionSheet *menu = [[UIActionSheet
alloc]

                          
initWithTitle: @""

                          
delegate:self

                          
cancelButtonTitle:@"Cancel"

                          
destructiveButtonTitle:nil

                          
otherButtonTitles:@"圖庫相簿",@"拍照",
nil];

    [menu
showInView:self.view];

# pragma mark UIActionSheet method

- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex

{

   
if (buttonIndex==0) {

        [self
photoalbumr];//圖庫相簿

    

    }else
if(buttonIndex==1){

         [self
photocamera];//拍照

    }else
if(buttonIndex==2){

    

    }

    

    [actionSheet
release];

}

調用系統的相簿

實現UIImagePickerControllerDelegate

-(void)photoalbumr{

   
if ([UIImagePickerController
isSourceTypeAvailable:

        
UIImagePickerControllerSourceTypePhotoLibrary]) {

       
UIImagePickerController *picker =

        [[UIImagePickerController
alloc]
init];

        picker.delegate =
self;

        picker.allowsImageEditing =
YES;

        picker.sourceType =
UIImagePickerControllerSourceTypePhotoLibrary;

        [self
presentModalViewController:picker animated:YES];

        [picker
release];

    }

   
else {

       
UIAlertView *alert = [[UIAlertView
alloc] 

                             
initWithTitle:@"Error accessing photo library" 

                             
message:@"Device does not support a photo library" 

                             
delegate:nil 

                             
cancelButtonTitle:@"Drat!" 

                             
otherButtonTitles:nil];

        [alert
show];

        [alert
release];

        

    }

}

-(void)photocamera{

   
if ([UIImagePickerController
isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {

       
UIImagePickerController* imagepicker = [[UIImagePickerController
alloc]
init];

        imagepicker.delegate =
self;

        imagepicker.sourceType =
UIImagePickerControllerSourceTypeCamera;

        imagepicker.modalTransitionStyle =
UIModalTransitionStyleCoverVertical;

        imagepicker.allowsEditing =
YES;

        [self
presentModalViewController:imagepicker
animated:YES];

    }

   
else {

       
UIAlertView *alert = [[UIAlertView
alloc] 

                             
initWithTitle:@"Sorry" 

                             
message:@"裝置不支援拍照功能" 

                             
delegate:nil 

                             
cancelButtonTitle:@"確定" 

                             
otherButtonTitles:nil];

        [alert
show];

        [alert
release];

        

    }

  }

#pragma mark UIImagePicker method

- (void)imagePickerController:(UIImagePickerController *)picker 

        didFinishPickingImage:(UIImage *)image 

                  editingInfo:(NSDictionary *)editingInfo

{  

 
/*將圖片儲存到document下

   
NSArray *paths =
NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,    
NSUserDomainMask, YES);

   
NSString *documentsDirectory = [paths objectAtIndex:0];

   
NSString *savedImagePath = [documentsDirectory
stringByAppendingPathComponent:@"savedImage.png"];

   
NSData *imageData = UIImagePNGRepresentation(image);

    [imageData
writeToFile:savedImagePath atomically:NO];   

     
_imgName=@"savedImage.png";

   
self._imgFilePath=documentsDirectory;

    */

  

    [addPhoto
setBackgroundImage:image forState:UIControlStateNormal];

   

     [picker
dismissModalViewControllerAnimated:YES];

}

// 完成選取

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{

    [picker
dismissModalViewControllerAnimated:YES];

}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.