ios - How to fetch squared thumbnails from PHImageManager? -


has idea how fetch squared thumbs phimagemanager? phimagecontentmodeaspectfill option has no effect.

[[phimagemanager defaultmanager]   requestimageforasset:(phasset *)_asset             targetsize:cgsizemake(80, 80)            contentmode:phimagecontentmodeaspectfill                options:nil          resulthandler:^(uiimage *result, nsdictionary *info) {     // sadly result not squared image     imageview.image = result; }]; 

update:

the bug in cropping images retrieved phimagemanager fixed in ios 8.3, version of ios , later, original example works, follows: seems bugs still there , including ios 8.4, can reproduce them standard iphone 6s camera images, taking full size square crop. fixed in ios 9.0, large crops of 63 megapixel panorama work fine.

the approach apple defines pass cgrect in co-ordinate space of image, origin (0,0) , maximum (1,1). pass rect in phimagerequestoptions object, along resizemode of phimagerequestoptionsresizemodeexact, , should cropped image.

- (void)showsquareimageforasset:(phasset *)asset {     nsinteger retinascale = [uiscreen mainscreen].scale;     cgsize retinasquare = cgsizemake(100*retinascale, 100*retinascale);      phimagerequestoptions *croptosquare = [[phimagerequestoptions alloc] init];     croptosquare.resizemode = phimagerequestoptionsresizemodeexact;      cgfloat cropsidelength = min(asset.pixelwidth, asset.pixelheight);     cgrect square = cgrectmake(0, 0, cropsidelength, cropsidelength);     cgrect croprect = cgrectapplyaffinetransform(square,                                                  cgaffinetransformmakescale(1.0 / asset.pixelwidth,                                                                             1.0 / asset.pixelheight));      croptosquare.normalizedcroprect = croprect;      [[phimagemanager defaultmanager]      requestimageforasset:(phasset *)asset      targetsize:retinasquare      contentmode:phimagecontentmodeaspectfit      options:croptosquare      resulthandler:^(uiimage *result, nsdictionary *info) {          self.imageview.image = result;      }]; } 

this example makes croprect of side length equal smaller of width , height of asset, , transforms co-ordinate space of image using cgrectapplyaffinetransform. may want set origin of square other (0,0), want crop square centred along axis of image being cropped, i'll leave exercise reader. :-)

original answer:

john's answer got me of way there, using code getting stretched , squashed images. here's how got imageview display square thumbnails fetched phimagemanager.

firstly, ensure contentmode property uiimageview set scaleaspectfill. default scaletofill, doesn't work correctly displaying square thumbnails phimagemanager, make sure change whether you've instantiated uiimageview in code or in storyboard.

//view dimensions based on points, we're requesting pixels phimagemanager nsinteger retinamultiplier = [uiscreen mainscreen].scale; cgsize retinasquare = cgsizemake(imageview.bounds.size.width * retinamultiplier, imageview.bounds.size.height * retinamultiplier);  [[phimagemanager defaultmanager]      requestimageforasset:(phasset *)_asset                targetsize:retinasquare               contentmode:phimagecontentmodeaspectfill                   options:nil             resulthandler:^(uiimage *result, nsdictionary *info) {      // result not square, correctly displays square using aspectfill     imageview.image = result; }]; 

specifying phimagerequestoptionsresizemodeexact resizemode not required, not give cropped image unless supply normalizedcroprect, , should not used here there's no benefit, , using means don't benefits of returned cached images.

the uiimage returned in result same aspect ratio source, scaled correctly use in uiimageview set aspect fill display square, if you're displaying it, way go. if need crop image print or export outside of app, isn't want - use of normalizedcroprect that. (edit- see below example of should work...)

except make sure set content mode of uiimageview uiviewcontentmodescaleaspectfill , set clipstobounds = yes following 2 lines :

imageview.contentmode=uiviewcontentmodescaleaspectfill; imageview.clipstobounds=yes; 

edit add normalizedcroprect usage example
warning - doesn't work, should according apple's documentation.

the approach apple defines pass cgrect in co-ordinate space of image, origin (0,0) , maximum (1,1). pass rect in phimagerequestoptions object, along resizemode of phimagerequestoptionsresizemodeexact, , should cropped image. problem don't, comes original aspect ratio , full image.

i've verified crop rect created correctly in image's co-ordinate space, , followed instruction use phimagerequestoptionsresizemodeexact, result handler still passed image in original aspect ratio. seems bug in framework, , when fixed, following code should work.

- (void)showsquareimageforasset:(phasset *)asset {     nsinteger retinascale = [uiscreen mainscreen].scale;     cgsize retinasquare = cgsizemake(100*retinascale, 100*retinascale);      phimagerequestoptions *croptosquare = [[phimagerequestoptions alloc] init];     croptosquare.resizemode = phimagerequestoptionsresizemodeexact;      cgfloat cropsidelength = min(asset.pixelwidth, asset.pixelheight);     cgrect square = cgrectmake(0, 0, cropsidelength, cropsidelength);     cgrect croprect = cgrectapplyaffinetransform(square,                                                  cgaffinetransformmakescale(1.0 / asset.pixelwidth,                                                                             1.0 / asset.pixelheight));      croptosquare.normalizedcroprect = croprect;      [[phimagemanager defaultmanager]      requestimageforasset:(phasset *)asset      targetsize:retinasquare      contentmode:phimagecontentmodeaspectfit      options:croptosquare      resulthandler:^(uiimage *result, nsdictionary *info) {          self.imageview.image = result;      }]; } 

all can suggest if have problem, file radar apple request fix it!


Comments

Popular posts from this blog

php - Submit Form Data without Reloading page -

linux - Rails running on virtual machine in Windows -