ios - What is the proper way to handle retina images from resized nsdata? -
when user selects image photo library, i'm resizing , uploading server , @ other point in app user can view photos. (i'm simplifying work flow)
the uiimageview on "detail" screen 320 x 320. based upon below method should using:
uiimage *image = [uiimage imagewithcgimage:img];
or
uiimage *image = [uiimage imagewithcgimage:img scale:[uiscreen mainscreen].scale orientation:img.imageorientation];
part b when request download image (nsdata) should use imagewithcgiimage
or imagewithcgiimage:scale:orientation
- (uiimage *)resizedimageforupload:(uiimage *)originalimage { static cgsize __maxsize = {640, 640}; nsmutabledata *data = [[nsmutabledata alloc] initwithdata:uiimagejpegrepresentation(originalimage, 1.0)]; cfmutabledataref dataref = (__bridge cfmutabledataref)data; cgimagesourceref imgsrc = cgimagesourcecreatewithdata(dataref, null); cgfloat width = [originalimage maxdimensionforconstraintsize:__maxsize]; nsnumber *maxwidth = [nsnumber numberwithfloat:width]; nsdictionary *options = @{ (__bridge nsstring *)kcgimagesourcecreatethumbnailfromimagealways : @yes, (__bridge nsstring *)kcgimagesourcecreatethumbnailwithtransform: @yes, (__bridge nsstring *)kcgimagesourcethumbnailmaxpixelsize : maxwidth }; cfdictionaryref cfoptions = (__bridge cfdictionaryref)options; cgimageref img = cgimagesourcecreatethumbnailatindex(imgsrc, 0, cfoptions); cfstringref type = cgimagesourcegettype(imgsrc); cgimagedestinationref imgdest = cgimagedestinationcreatewithdata(dataref, type, 1, null); cgimagedestinationaddimage(imgdest, img, null); cgimagedestinationfinalize(imgdest); uiimage *image = [uiimage imagewithcgimage:img]; cfrelease(imgsrc); cgimagerelease(img); cfrelease(imgdest); return image; }
it appears i've found answer own question. resizeimageforupload
shouldn't try scale based upon device. since i'm defining max size 640,640 (retina size 320,320 uiimageview) no other manipulation necessary. i've added caching images , i'm handing scaling @ point:
uiimage *image = [uiimage imagewithdata:imgdata scale:[uiscreen mainscreen].scale];
and return. reason why thought had messed was trying scale already scaled image. lessons learned.
Comments
Post a Comment