ios - App crashes with 'NSRangeException' when library is empty -
i using code latest photo cameraroll:
- (void) getthelatestphoto{ alassetslibrary *cameraroll = [[alassetslibrary alloc] init]; [cameraroll enumerategroupswithtypes:alassetsgroupsavedphotos usingblock:^(alassetsgroup *images, bool *stop) { [images setassetsfilter:[alassetsfilter allphotos]]; [images enumerateassetsatindexes:[nsindexset indexsetwithindex:[images numberofassets] - 1] options:0 usingblock:^(alasset *result, nsuinteger index, bool *stop) { if (result) { alassetrepresentation *rep = [result defaultrepresentation]; self.sharingimage = [uiimage imagewithcgimage:[rep fullscreenimage]]; } }]; } failureblock: ^(nserror *error) { nslog(@"an error occured: %@",error); }]; } as is, if library cameraroll empty, app crash error:
terminating app due uncaught exception 'nsrangeexception', reason: 'indexset count or lastindex must not exceed -numberofassets' i understand indexset higher numberofassets due this:
[images enumerateassetsatindexes:[nsindexset indexsetwithindex:[images numberofassets] - 1] at point question "where put condition allow me check if assets zero?"
if cameraroll has more zero, let code continue, if count zero, handle issue way oppose let crash:
uialertview *alert = [[uialertview alloc]initwithtitle:@"" message:@"sorry, there no photo in camera roll" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alert show]; thanks help!
add if([images numberofassets]>0) before enumerateassetsatindexes line , continue code...
or if([images numberofassets]<=0)
uialertview *alert = [[uialertview alloc]initwithtitle:@"" message:@"sorry, there no photo in camera roll" delegate:nil cancelbuttontitle:@"ok" otherbuttontitles:nil, nil]; [alert show];
Comments
Post a Comment