//初始化picker
- (UIImagePickerController *)picker
{
_picker = [[UIImagePickerController alloc]init];
_picker.sourceType = UIImagePickerControllerSourceTypeCamera;//设置image picker的来源
_picker.cameraDevice = UIImagePickerControllerCameraDeviceRear;//设置使
_picker.cameraCaptureMode = UIImagePickerControllerCameraCaptureModePhoto;
_picker.allowsEditing=YES; //允许编辑
_picker.delegate=self; //设置代理,检测操作
return _picker;
}
#pragma mark - 代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
NSString *mediaType=[info objectForKey:UIImagePickerControllerMediaType];
if ([mediaType isEqualToString:(NSString *)kUTTypeImage]) {//当是拍照时
UIImage *image;
//如果允许编辑则获得编辑后的照片,否则获取原始照片
if (self.picker.allowsEditing) {
image = [info objectForKey:UIImagePickerControllerEditedImage];//获取编辑后的照片
}else{
image = [info objectForKey:UIImagePickerControllerOriginalImage];//获取原始照片
}
// 4 插入数据
NSString * insertSql= @"INSERT INTO A_camera(cameradata,message)VALUES(?,?)";
NSData *data = UIImagePNGRepresentation(image);
// 插入语句
bool inflag1=[dataBase executeUpdate:insertSql,data,@"空"];
if(inflag1){
NSLog(@"插入数据成功");
[self selectForms];
[self.collect reloadData];
}
}
// 关闭照片选择器
[self dismissViewControllerAnimated:YES completion:nil];
[self.collect reloadData];
}
//定义每个UICollectionView 的 margin
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
return UIEdgeInsetsMake(15, 50, 5, 20);
}
- (CGFloat) collectionView:(UICollectionView *)collectionView
layout:(UICollectionViewLayout *)collectionViewLayout
minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
return 46.0f;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
return self.cameraDataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(nonnull NSIndexPath *)indexPath
{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cellID" forIndexPath:indexPath];
self.CameralookView.photoImageView.image = [UIImage imageWithData:self.cameraDataArray[indexPath.row]];
cell.backgroundView = self.CameralookView;
return cell;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell * cell = (UICollectionViewCell *)[collectionView cellForItemAtIndexPath:indexPath];
if (cell) {
secondViewController *sec = [[secondViewController alloc]init];
sec.image = self.cameraDataArray[indexPath.row];
[self presentViewController:sec animated:YES completion:nil];
}else{NSLog(@"dd");}
}