objective c - iOS - Get File Size of Image Selected From Photo Reel -
i'm building app allows users select photos , videos device , upload them server. trying file size (in bytes) of each item select, can me out?
if ([dict objectforkey:uiimagepickercontrollermediatype] == alassettypephoto){ // image file if ([dict objectforkey:uiimagepickercontrolleroriginalimage]){ nsurl* urlpath=[dict objectforkey:@"uiimagepickercontrollerreferenceurl"]; item = [bundleitem itemwithpath:urlpath anddescription:nil]; item.itemimage = [dict objectforkeyedsubscript:uiimagepickercontrolleroriginalimage]; item.itemtype = 1; // image item.itemsize = // need here?? [m_items addobject:item]; } } else if ([dict objectforkey:uiimagepickercontrollermediatype] == alassettypevideo){ // video file if ([dict objectforkey:uiimagepickercontrolleroriginalimage]){ nsurl* urlpath=[dict objectforkey:@"uiimagepickercontrollerreferenceurl"]; item = [bundleitem itemwithpath:urlpath anddescription:nil]; item.itemimage = [dict objectforkeyedsubscript:uiimagepickercontrolleroriginalimage]; item.itemtype = 2; // video item.itemsize = // need here?? [m_items addobject:item]; } }
edit
getting nscocaoerrordomain 256 videos:
nsurl* urlpath=[dict objectforkey:@"uiimagepickercontrollerreferenceurl"]; item = [bundleitem itemwithpath:urlpath anddescription:nil]; item.itemimage = [dict objectforkeyedsubscript:uiimagepickercontrolleroriginalimage]; item.itemtype = 2; // video //error container nserror *attributeserror; nsdictionary *fileattributes = [[nsfilemanager defaultmanager] attributesofitematpath:[urlpath path] error:&attributeserror]; nsnumber *filesizenumber = [fileattributes objectforkey:nsfilesize]; long filesize = [filesizenumber longvalue]; item.itemsize = filesize; [m_items addobject:item];
for image data selection:
item.itemimage = (uiimage*)[info valueforkey:uiimagepickercontrolleroriginalimage]; nsdata *imgdata = uiimagejpegrepresentation(item.itemimage, 1); //1 represents quality of image. nslog(@"size of image(bytes):%d",[imgdata length]);
hope you.
below method generalize, work both image , video:
something should take care finding file size of selected image or video returned uiimagepickercontroller
- (void)imagepickercontroller:(uiimagepickercontroller *)picker didfinishpickingmediawithinfo:(nsdictionary *)info { nsurl *videourl=(nsurl*)[info objectforkey:uiimagepickercontrollermediaurl]; //error container nserror *attributeserror; nsdictionary *fileattributes = [[nsfilemanager defaultmanager] attributesofitematpath:[videourl path] error:&attributeserror]; nsnumber *filesizenumber = [fileattributes objectforkey:nsfilesize]; long long filesize = [filesizenumber longlongvalue]; }
Comments
Post a Comment