ios 自定义相册开发

本文详细介绍了如何在iOS应用中实现自定义相册功能,包括初始化UIImagePickerController、设置相机参数、实现代理方法以处理拍照后的图片,并将图片保存到数据库。此外,还展示了使用UICollectionView展示相册内容的方法,包括布局设置、数据源方法和点击事件处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

//初始化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");}

}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值