從手機相冊中選取圖片

也是看到簡書里一哥們的,忘了是誰的了,只是他不會排版,我又給整理了一下!


// 打開相冊
- (IBAction)openPhotoLibiary:(UIButton *)sender

{

    //打開相冊

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

    //資源類型為圖片庫

    picker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;

    picker.delegate = self;

    //設(shè)置選擇后的圖片可被編輯

    picker.allowsEditing = YES;

    [self presentViewController:picker animated:YES completion:nil];

}

pragma Delegate - 相冊 UIImagePickerControllerDelegate

//圖像選取器的委托方法,選完圖片后回調(diào)該方法


-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{

//當(dāng)圖片不為空時顯示圖片并保存圖片

if (image != nil) {

//圖片顯示在界面上

        [changeImg setBackgroundImage:image forState:UIControlStateNormal];

//以下是保存文件到沙盒路徑下

//把圖片轉(zhuǎn)成NSData類型的數(shù)據(jù)來保存文件

NSData *data;

//判斷圖片是不是png格式的文件

if (UIImagePNGRepresentation(image)) {

//返回為png圖像。

data = UIImagePNGRepresentation(image);

}else {

//返回為JPEG圖像。

      data = UIImageJPEGRepresentation(image, 1.0);

}

//保存

//        [[NSFileManager defaultManager] createFileAtPath:self.imagePath contents:data attributes:nil];

}

//關(guān)閉相冊界面

[picker dismissModalViewControllerAnimated:YES];
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容