Storage of iOS Development--uiimage data
Last Update:2017-05-27
Source: Internet
Author: User
<span id="Label3"></p><p><p>1. Save pictures and Directions</p></p><p><p>[userdefault setobject:uiimagepngrepresentation (image) forkey:uduserimage];</p></p><p><p>[userdefault Setobject:[nsnumber numberWithInteger:image.imageOrientation] forkey:uduserimageorientation];</p></p><p><p></p></p><p><p>2. Get pictures and directions</p></p><p><p>nsdata* ImageData = [userdefault objectforkey:uduserimage];</p></p><p><p>UIImage *userimage = [UIImage imagewithdata:imagedata];</p></p><p><p>Nsinteger imageorientation = [[userdefault objectforkey:uduserimageorientation] integervalue];</p></p><p><p></p></p><p><p>3. Display pictures</p></p><p><p>Imageview.image = [self Fixorientationwithimage:userimage andimageorientation:imageorientation];</p></p><p><p>-(UIImage *) fixorientationwithimage: (UIImage *) aimage andimageorientation: (nsinteger) imageorientation {</p></p><p><p>if (imageorientation = = Uiimageorientationup)</p></p><p><p>Return aimage;</p></p><p><p></p></p><p><p>Cgaffinetransform transform = cgaffinetransformidentity;</p></p><p><p></p></p><p><p>Switch (imageorientation) {</p></p><p><p>Case Uiimageorientationdown:</p></p><p><p>Case Uiimageorientationdownmirrored:</p></p><p><p>Transform = cgaffinetransformtranslate (transform, aImage.size.width, aImage.size.height);</p></p><p><p>Transform = cgaffinetransformrotate (transform, m_pi);</p></p><p><p>Break</p></p><p><p></p></p><p><p>Case Uiimageorientationleft:</p></p><p><p>Case Uiimageorientationleftmirrored:</p></p><p><p>Transform = cgaffinetransformtranslate (transform, aImage.size.width, 0);</p></p><p><p>Transform = cgaffinetransformrotate (transform, m_pi_2);</p></p><p><p>Break</p></p><p><p></p></p><p><p>Case Uiimageorientationright:</p></p><p><p>Case Uiimageorientationrightmirrored:</p></p><p><p>Transform = cgaffinetransformtranslate (transform, 0, aImage.size.height);</p></p><p><p>Transform = Cgaffinetransformrotate (transform,-m_pi_2);</p></p><p><p>Break</p></p><p><p>Default</p></p><p><p>Break</p></p><p><p>}</p></p><p><p></p></p><p><p>Switch (imageorientation) {</p></p><p><p>Case Uiimageorientationupmirrored:</p></p><p><p>Case Uiimageorientationdownmirrored:</p></p><p><p>Transform = cgaffinetransformtranslate (transform, aImage.size.width, 0);</p></p><p><p>Transform = Cgaffinetransformscale (transform,-1, 1);</p></p><p><p>Break</p></p><p><p></p></p><p><p>Case Uiimageorientationleftmirrored:</p></p><p><p>Case Uiimageorientationrightmirrored:</p></p><p><p>Transform = cgaffinetransformtranslate (transform, aImage.size.height, 0);</p></p><p><p>Transform = Cgaffinetransformscale (transform,-1, 1);</p></p><p><p>Break</p></p><p><p>Default</p></p><p><p>Break</p></p><p><p>}</p></p><p><p></p></p><p><p>Cgcontextref CTX = cgbitmapcontextcreate (NULL, aImage.size.width, aImage.size.height,</p></p><p><p>Cgimagegetbitspercomponent (aimage.cgimage), 0,</p></p><p><p>Cgimagegetcolorspace (aimage.cgimage),</p></p><p><p>Cgimagegetbitmapinfo (aimage.cgimage));</p></p><p><p>CGCONTEXTCONCATCTM (ctx, transform);</p></p><p><p>Switch (imageorientation) {</p></p><p><p>Case Uiimageorientationleft:</p></p><p><p>Case Uiimageorientationleftmirrored:</p></p><p><p>Case Uiimageorientationright:</p></p><p><p>Case Uiimageorientationrightmirrored:</p></p><p><p>Grr ...</p></p><p><p>Cgcontextdrawimage (ctx, CGRectMake (0,0,aimage.size.height,aimage.size.width), aimage.cgimage);</p></p><p><p>Break</p></p><p><p></p></p><p><p>Default</p></p><p><p>Cgcontextdrawimage (ctx, CGRectMake (0,0,aimage.size.width,aimage.size.height), aimage.cgimage);</p></p><p><p>Break</p></p><p><p>}</p></p><p><p></p></p><p><p>Cgimageref cgimg = Cgbitmapcontextcreateimage (ctx);</p></p><p><p>UIImage *img = [UIImage imagewithcgimage:cgimg];</p></p><p><p>Cgcontextrelease (ctx);</p></p><p><p>Cgimagerelease (cgimg);</p></p><p><p>Return img;</p></p><p><p>}</p></p><p><p>Storage of iOS Development--uiimage data</p></p></span>