iphone - CGContextAddEllipseInRect to CGImageRef to CGImageMaskCreate to CGContextClipToMask -
i haven't been able find 1 single example on internets teaches me how create circle on fly , use circle clip uiimage.
here's code, unfortunately doesn't give me desired results.
//create graphics context uigraphicsbeginimagecontext(cgsizemake(243, 243)); cgcontextref context = uigraphicsgetcurrentcontext(); //create object in context cgcontextaddellipseinrect(context, cgrectmake(0, 0, 243, 243)); cgcontextsetfillcolor(context, cgcolorgetcomponents([[uicolor whitecolor] cgcolor])); cgcontextfillpath(context); uiimage *image = uigraphicsgetimagefromcurrentimagecontext(); //create uiimage ellipse //get drawing image cgimageref maskimage = [image cgimage]; // mask image cgimageref maskref = cgimagemaskcreate(cgimagegetwidth(maskimage) , cgimagegetheight(maskimage) , cgimagegetbitspercomponent(maskimage) , cgimagegetbitsperpixel(maskimage) , cgimagegetbytesperrow(maskimage) , cgimagegetdataprovider(maskimage) , null , false); //finally clip context mask. cgcontextcliptomask( context , cgrectmake(0, 0, 243, 243) , maskref ); //draw image [firstpieceview.image drawinrect:cgrectmake(0, 0, 320, 480)]; // [firstpieceview drawrect:cgrectmake(0, 0, 320, 480)]; //extract new image uiimage *outputimage = uigraphicsgetimagefromcurrentimagecontext(); nslog(@"self.firstpieceview %@", nsstringfromcgrect(self.firstpieceview.frame)); uigraphicsendimagecontext(); self.firstpieceview.image = outputimage;
i appreciate directions.
i suspect need rephrase question better. there's plenty of example code whatever you're trying out there.
here's how implement custom uiview
subclass clip image ellipse:
- (void)drawinrect:(cgrect)rect { uiimage image;// set/get somewhere cgimageref imageref = [image cgimageref]; cgcontextref context = uigraphicsgetcurrentcontext(); cgcontextaddellipseinrect(context, self.bounds); cgcontextclip(context); cgcontextdrawimage(context, self.bounds, imageref); }
caveat emptor
edit (a day later, free time produces):
- (void)drawrect:(cgrect)rect { // we're ignoring rect , drawing whole view cgimageref imageref = [_image cgimage]; // ivar: uiimage *_image; cgcontextref context = uigraphicsgetcurrentcontext(); // set background black [[uicolor blackcolor] setfill]; cgcontextfillrect(context, self.bounds); // modify context coordinates, // uikit , coregraphics oriented differently cgcontextsavegstate(context); cgcontexttranslatectm(context, 0, cgrectgetheight(rect)); cgcontextscalectm(context, 1, -1); // add clipping path context, execute clip // in effect drawing until gstate restored cgcontextaddellipseinrect(context, self.bounds); cgcontextclip(context); // stretch image size of view cgcontextdrawimage(context, self.bounds, imageref); cgcontextrestoregstate(context); }
Comments
Post a Comment