ios - How to save selected images from gallery through ALAssetsLibrary to document directory of app -
i have fetched images gallery collection view through alassetslibrary . , have done successfully.
now, want save selected image collection view document directory of app.
and selecting images code--
-(void)done { nsmutablearray *imagearray = [[nsmutablearray alloc] initwithcapacity:[selectedindexarr count]]; [selectedindexarr enumerateobjectsusingblock:^(id obj, nsuinteger idx, bool *stop) { alassetslibrary *library = [[alassetslibrary alloc] init]; [library assetforurl:obj resultblock:^(alasset *asset) { alassetrepresentation *representation = [asset defaultrepresentation]; uiimage *latestphoto = [uiimage imagewithcgimage:[representation fullscreenimage]]; [imagearray addobject:latestphoto]; if ([imagearray count]==[selectedindexarr count]) { if ([self.delegate respondstoselector:@selector(ims_pickercontroller:didfinishpickingmediaitems:)]) { [self.delegate ims_pickercontroller:self didfinishpickingmediaitems:imagearray]; } } [self saveimagesfromlibrarytodocumentdirectory:imagearray]; } failureblock:^(nserror *error) { nslog(@"failure picking media"); }]; }]; }
and both of these working when trying save in document directory getting error @ line:
nsstring *docpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)objectatindex:0]; nsstring* filename = [docpath stringbyappendingpathcomponent:[representation filename]];
terminating app due uncaught exception 'nsinvalidargumentexception', reason: '-[uiimage defaultrepresentation]: unrecognized selector sent instance 0x7afe6150' *** first throw call stack: (
0 corefoundation 0x01a28946 __exceptionpreprocess + 182 1 libobjc.a.dylib 0x016b1a97 objc_exception_throw + 44 2 corefoundation 0x01a305c5 -[nsobject(nsobject) doesnotrecognizeselector:] + 277 3 corefoundation 0x019793e7 ___forwarding___ + 1047 4 corefoundation 0x01978fae _cf_forwarding_prep_0 + 14 5 moviemixture1 0x00067571 -[imageselector saveimagesfromlibrarytodocumentdirectory:] + 177 6 moviemixture1 0x000671b1 __21-[imageselector done]_block_invoke_2 + 513 7 assetslibrary 0x0013a216 __56-[alassetslibrary assetforurl:resultblock:failureblock:]_block_invoke_2 + 215 8 libdispatch.dylib 0x056eb30a _dispatch_call_block_and_release + 15 9 libdispatch.dylib 0x0570be2f _dispatch_client_callout + 14 10 libdispatch.dylib 0x056f290e _dispatch_main_queue_callback_4cf + 606 11 corefoundation 0x0198295e __cfrunloop_is_servicing_the_main_dispatch_queue__ + 14 12 corefoundation 0x01941760 __cfrunlooprun + 2256 13 corefoundation 0x01940bcb cfrunlooprunspecific + 443 14 corefoundation 0x019409fb cfrunloopruninmode + 123 15 graphicsservices 0x04b3324f gseventrunmodal + 192 16 graphicsservices 0x04b3308c gseventrun + 104 17 uikit 0x020198b6 uiapplicationmain + 1526 18 moviemixture1 0x0007dc4d main + 141 19 libdyld.dylib 0x05737ac9 start + 1
)
please me through this.
my code saving images:
-(void)saveimagesfromlibrarytodocumentdirectory:(nsarray *)imagearray { (int j=0; j<[imagearray count]; j++) { alassetrepresentation *representation = [[imagearray objectatindex:j] defaultrepresentation]; nsstring *docpath = [nssearchpathfordirectoriesindomains(nsdocumentdirectory, nsuserdomainmask, yes)objectatindex:0]; nsstring* filename = [docpath stringbyappendingpathcomponent:[representation filename]]; [[nsfilemanager defaultmanager] createfileatpath:filename contents:nil attributes:nil]; nsoutputstream *outputstream = [nsoutputstream outputstreamtofileatpath:filename append:yes]; [outputstream open]; long long offset = 0; long long bytesread = 0; nserror *error; uint8_t * buffer = malloc(131072); while (offset<[representation size] && [outputstream hasspaceavailable]) { bytesread = [representation getbytes:buffer fromoffset:offset length:131072 error:&error]; [outputstream write:buffer maxlength:bytesread]; offset = offset+bytesread; } [outputstream close]; free(buffer); } }
Comments
Post a Comment