iphone - Crop letter from image using Masking -
what need:
1) choose image library or camera
2) write , text
3) text cropped image!
below image can clarify more on need.
i know masking , cropping of image did masking frame in emoji me app . need know how image should cropped according dynamic text.
please give suggestions.
... uiimage *image = [uiimage imagenamed:@"dogs.png"]; uiimage *mask = [uiimage imagenamed:@"mask.png"]; // result of masking method uiimage *maskedimage = [self maskimage:image withmask:mask]; ... - (uiimage*) maskimage:(uiimage *)image withmask:(uiimage *)maskimage { cgimageref maskref = maskimage.cgimage; cgimageref mask = cgimagemaskcreate(cgimagegetwidth(maskref), cgimagegetheight(maskref), cgimagegetbitspercomponent(maskref), cgimagegetbitsperpixel(maskref), cgimagegetbytesperrow(maskref), cgimagegetdataprovider(maskref), null, false); cgimageref maskedimageref = cgimagecreatewithmask([image cgimage], mask); uiimage *maskedimage = [uiimage imagewithcgimage:maskedimageref]; cgimagerelease(mask); cgimagerelease(maskedimageref); // returns new image mask applied return maskedimage; }
1) write image text below code
uiimage *textedimage = [self imagefromtext:@"text show"]; -(uiimage *)imagefromtext:(nsstring *)text { //set width string wrap. cgsize maximumsize; maximumsize = cgsizemake(320, 300); //set text image font here uifont *font = [uifont boldsystemfontofsize:50]; cgsize strsize1 = [text sizewithfont:font constrainedtosize:maximumsize linebreakmode:uilinebreakmodewordwrap]; cgsize strsize =cgsizemake(320, strsize1.height); if (uigraphicsbeginimagecontextwithoptions != null) uigraphicsbeginimagecontextwithoptions(strsize,no,0.0); else uigraphicsbeginimagecontext(strsize); //set new text iamge frame here cgrect newframe = cgrectmake(0, 0, 320, 400); uicolor *color = [uicolor redcolor]; [color set]; [text drawinrect:newframe withfont:font linebreakmode:uilinebreakmodecharacterwrap alignment:uitextalignmentcenter]; uiimage *textimg = uigraphicsgetimagefromcurrentimagecontext(); uigraphicsendimagecontext(); return textimg; }
2) after getting texture image apply image making required image
uiimage *maskedimage = [self maskimage:finalimage withmask: textedimage]; - (uiimage*) maskimage:(uiimage *)image withmask:(uiimage *)maskimage { cgcolorspaceref colorspace = cgcolorspacecreatedevicergb(); //uiimage *maskimage = [uiimage imagenamed:@"mask.png"]; cgimageref maskimageref = [maskimage cgimage]; // create bitmap graphics context size of image cgcontextref mainviewcontentcontext = cgbitmapcontextcreate (null, maskimage.size.width, maskimage.size.height, 8, 0, colorspace, kcgimagealphapremultipliedlast); if (mainviewcontentcontext==null) return null; cgfloat ratio = 0; ratio = maskimage.size.width/ image.size.width; if(ratio * image.size.height < maskimage.size.height) { ratio = maskimage.size.height/ image.size.height; } cgrect rect1 = {{0, 0}, {maskimage.size.width, maskimage.size.height}}; cgrect rect2 = {{-((image.size.width*ratio)-maskimage.size.width)/2 , -((image.size.height*ratio)-maskimage.size.height)/2}, {image.size.width*ratio, image.size.height*ratio}}; cgcontextcliptomask(mainviewcontentcontext, rect1, maskimageref); cgcontextdrawimage(mainviewcontentcontext, rect2, image.cgimage); // create cgimageref of main view bitmap content, , // release bitmap context cgimageref newimage = cgbitmapcontextcreateimage(mainviewcontentcontext); cgcontextrelease(mainviewcontentcontext); uiimage *theimage = [uiimage imagewithcgimage:newimage]; cgimagerelease(newimage); // return image return theimage; }
Comments
Post a Comment