ios - How to get requested image size for iOS8 PhotoKit? -
i using code below fetching image:
[[phimagemanager defaultmanager] requestimageforasset:asset targetsize:cgsizemake(800, 600) contentmode:phimagecontentmodeaspectfill options:options resulthandler:^(uiimage *result, nsdictionary *info) { nslog(@"size:%@",nsstringfromcgsize(result.size)); }];
i'm requesting image size of 800 x 600, i'm getting image size of 45 x 60, poor-quality.
how can requested image size using photokit
?
your (unnati's) self answer work, misses out on number of optimisations apple give in framework images display more quickly.
your original code pretty correct, include options
object don't need, , may have been causing problems if had set resizemode
incorrectly on it. here code should work you, i've removed options object.
[[phimagemanager defaultmanager] requestimageforasset:(phasset *)asset targetsize:cgsizemake(800, 600) contentmode:phimagecontentmodeaspectfit options:nil resulthandler:^(uiimage *result, nsdictionary *info) { nslog(@"image size:%@",nsstringfromcgsize(result.size)); }];
this should simpler, , give optimisations apple provide framework free. here's explanation of get:
responding low quality image
the framework call result handler multiple times increasing quality images, allowing display user while wait final image. the documentation:
photos may call result handler block more once. photos first calls block provide low-quality image suitable displaying temporarily while prepares high-quality image. (if low-quality image data available, first call may occur before method returns.) when high-quality image ready, photos calls result handler again provide it.
this means resulthandler
block needs safe call more once - block in code logs size, , test image 2 log messages, first 1 low quality 40x26 px image, , again high quality image. if open ios photos app, can see low quality images briefly before appear sharpen - what's happening here.
using options.deliverymode = phimagerequestoptionsdeliverymodehighqualityformat
prevents behaviour, , resulthandler
called once final image, suggest not setting option unless can't devise handler block (in real code) can called multiple times.
returning cached high quality image quickly
to provide final image quickly, framework provide image close requested size in final call resulthandler
block, if has 1 cached already. if you're using uiimageview
set contentmode
of aspect fill or fit, won't problem, specify matching contentmode
in image request. example, against test image, second call resulthandler
1280x850 image, larger requested right ballpark. however, setting options.resizemode = phimagerequestoptionsresizemodeexact
prevent behaviour , force framework resize fresh copy of image rather using cached copy, takes time.
since photos framework doesn't crop images (even if ask to, that's bug ;-)), won't exact size request, using phimagerequestoptionsresizemodeexact
option. example, if request 800x600 image not in 4:3 ratio, photos can't return image of size, gets close can - on latest image, request gets image of 800x531. means need able handle images aren't exact size request, might take advantage of optimisations in framework , avoid using phimagerequestoptionsresizemodeexact
. the documentation again:
resizing match target size less efficient using fast resizing option.
(did have set phimagerequestoptionsresizemodefast
in original options
object, not included in question? may have been causing issue.)
image versions
it's possible request different versions of image, in self answer options.version = phimagerequestoptionsversioncurrent
. 1 not required however, default current version.
Comments
Post a Comment