iOS 把图片保存到相册,并获取图片文件名的实例
程序员文章站
2023-12-17 11:03:16
实例如下所示:
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinish...
实例如下所示:
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { uiimage *image = [info objectforkey:uiimagepickercontrolleroriginalimage]; if (picker.sourcetype == uiimagepickercontrollersourcetypecamera) { uiimagewritetosavedphotosalbum(image,self,@selector(image:didfinishsavingwitherror:contextinfo:),null); } } - (void)image:(uiimage*)image didfinishsavingwitherror:(nserror*)error contextinfo:(void*)contextinfo { if (error) { dlog(@"图片保存失败!"); }else{ dlog(@"图片保存成功!"); } } 此方法可以把其他来源的图片也可保存到相册 //当image从相机中获取的时候存入相册中 //保存方法1 // uiimagewritetosavedphotosalbum(img.image,self,@selector(image:didfinishsavingwitherror:contextinfo:),null); // imagedata = nil; weakself //保存方法2同时获取保存的相片文件名 __blockalassetslibrary *lib = [[alassetslibraryalloc] init]; [lib writeimagetosavedphotosalbum:img.image.cgimagemetadata:nilcompletionblock:^(nsurl *asseturl,nserror *error) { alassetslibraryassetforurlresultblock resultblock = ^(alasset *imageasset) { alassetrepresentation *imagerep = [imageassetdefaultrepresentation]; weakself.imagename = [nsstringstringwithformat:@"%@",[imagerepfilename]]; nslog(@"[imagerep filename 1] : %@", [imagerepfilename]); dblog(@"self.addressimagename照相0 = %@",weakself.imagename); commitblock(img.image,uiimagejpegrepresentation(img.image,0.5),self.imagename); }; alassetslibrary* assetslibrary = [[alassetslibraryalloc] init]; [assetslibrary assetforurl:asseturlresultblock:resultblock failureblock:nil]; nslog(@"asseturl = %@, error = %@", asseturl, error); lib = nil; }]; }];
以上这篇ios 把图片保存到相册,并获取图片文件名的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。