#import <UIKit/UIKit.h>
@interfaceYBPhotoSelectView :UIView
@property(nonatomic,weak)id target;
@end
#import"YBPhotoSelectView.h"
#import <Masonry.h>
#import"GlobalDefine.h"
@interfaceYBPhotoSelectView()
@property(nonatomic,weak)UIImageView* photoView;
@end
@implementationYBPhotoSelectView
- (instancetype)initWithFrame:(CGRect)frame {
if(self= [super initWithFrame:frame])
{
[self commonInit];
}
return self;
}
- (void)commonInit {
UIImageView* photoView
= [[UIImageView alloc]init];
photoView.image= [UIImage imageNamed:@"dog"];
photoView.userInteractionEnabled=YES;
[self addSubview:photoView];
self.photoView= photoView;
[photoView mas_makeConstraints:^(MASConstraintMaker*make) {
make.edges.equalTo(self);
}];
UITapGestureRecognizer*
tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onClickedPhoto)];
[photoView addGestureRecognizer:tap];
}
- (void)onClickedPhoto {
UIActionSheet*popMenu = [[UIActionSheetalloc] initWithTitle:@"選擇照片" delegate:self
cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"相冊(cè)",nil];
UIViewController* vc = (UIViewController*)self.target;?
[popMenu showInView:vc.view];
}
#pragma mark - actionSheet delegate methods
-(void)getMediaFromSource : (UIImagePickerControllerSourceType) sourceType
{
NSArray*mediaTypes =
[UIImagePickerController availableMediaTypesForSourceType:sourceType];
if([UIImagePickerController isSourceTypeAvailable:sourceType]
&& [mediaTypes count] >0)
{
UIImagePickerController*picker = [[UIImagePickerController alloc]init];
NSMutableArray*types = [NSMutableArray array];
for(NSString*type in mediaTypes){
if([typecompare:@"public.movie"]!=NSOrderedSame){
[types addObject:type];
}
}
picker.mediaTypes= types;
picker.delegate=self;
picker.allowsEditing=YES;
picker.sourceType= sourceType;
[self.target presentViewController:pickeranimated:YES ?completion:nil];
}
else
{
UIAlertView*alert = [[UIAlertViewalloc]init];
[alert setTitle:NSLocalizedString(@"note_tishi",@"")];
[alert setMessage:@"您的設(shè)備不支持該功能蚕捉。"];
[alert setDelegate:nil];
[alert addButtonWithTitle:@"知道了"];
[alert show];
}
}
-(void) actionSheet:(UIActionSheet*)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSString*title = [actionSheet buttonTitleAtIndex:buttonIndex];
if([title is EqualToString:@"拍照"])
{
[self getMediaFromSource:UIImagePickerControllerSourceTypeCamera];
}
elseif([title is EqualToString:@"相冊(cè)"])
{
[self getMediaFromSource:UIImagePickerControllerSourceTypePhotoLibrary];
}
}
#pragmamark - UIImagePickerController delegate methods
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info
{
UIImage*chosenImage =
[info objectForKey:UIImagePickerControllerEditedImage];
if(picker.sourceType==UIImagePickerControllerSourceTypeCamera){
}
self.photoView.image= chosenImage;
[self.tar getdismissViewControllerAnimated:YEScompletion:nil];
}
- (void)image:(UIImage*)image didFinishSavingWithError:(NSError*)error contextInfo:(void*)contextInfo
{
if(error !=NULL)
{
}
else// No errors
{
NSLog(@"image successfully saved");
}
}
-(void) imagePickerControllerDidCancel:(UIImagePickerController*)picker
{
[self.target dismissViewControllerAnimated:YEScompletion:nil];
}
@end