2012-03-26 10 views
9

मैं छवि पर खींचे गए शब्द को एक छवि फ़िल्टर या मुखौटा लागू करना चाहता हूं। शब्द पृष्ठभूमि छवि के माध्यम से देखने के लिए पारदर्शी प्रभाव होगा। क्या मूल आईओएस एसडीके में यह संभव है या मुझे इसे करने के लिए अलग-अलग एपीआई की आवश्यकता है। इस छवि में 2 छवियां हैं। एक वह जगह है जहां भारत लिखा गया है, और दूसरा एक भारत पत्र के माध्यम से देखता है। This image consist of 2 images. one is where India is written over, and another one is which is see through the India letter.आईओएस एसडीके में एक छवि को कैसे मुखौटा करें?

यह वह कोड है जिसका उपयोग मैं टेक्स्ट से छवि उत्पन्न करने के लिए कर रहा हूं।

-(UIImage *)imageFromText:(NSString *)text{ 
// set the font type and size 
UIFont *font = [UIFont systemFontOfSize:100.0]; 
CGSize size = [text sizeWithFont:font]; 

// check if UIGraphicsBeginImageContextWithOptions is available (iOS is 4.0+) 
if (UIGraphicsBeginImageContextWithOptions != NULL) 
    UIGraphicsBeginImageContextWithOptions(size,NO,0.0); 
else 
    // iOS is < 4.0 
    UIGraphicsBeginImageContext(size); 

// optional: add a shadow, to avoid clipping the shadow you should make the context size bigger 
// 
CGContextRef ctx = UIGraphicsGetCurrentContext(); 
CGContextSetShadowWithColor(ctx, CGSizeMake(0.0, 1.0), 5.0, [[UIColor blackColor] CGColor]); 
CGContextSetBlendMode(ctx,kCGBlendModeNormal); 
CGContextSetFillColorWithColor(ctx, [UIColor whiteColor].CGColor); 


/*NSLog(@"Rect %@",CGContextGetClipBoundingBox(ctx)); 
CGImageRef alphaMask = CGBitmapContextCreateImage(ctx); 
CGContextClipToMask(ctx, CGContextGetClipBoundingBox(ctx), alphaMask);*/ 


// draw in context, you can use also drawInRect:withFont: 
[text drawAtPoint:CGPointMake(0.0, 0.0) withFont:font]; 

// transfer image 
UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext();  

return image;} 

यह ठीक काम कर रहा है, लेकिन मैं छवि है जो काले रंग की पृष्ठभूमि और पारदर्शी पाठ के माध्यम से यह देखने के लिए होगा उत्पन्न करने के लिए की जरूरत है।

+0

तुम लोगों को धन्यवाद। यह काम कर रहा है। 2 छवियों को हमें केवल एक की जरूरत है काले रंग की पृष्ठभूमि और पत्र एम्बेडेड के साथ मुखौटा छवि है और दूसरा मूल छवि है जिस पर मास्किंग काम करेगी। कोड से गतिशील रूप से मास्किंग छवि बनाना संभव हो सकता है? – arindam

उत्तर

13

आप इस कोड (here से मिल गया) का उपयोग कर सकते हैं,

- (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 masked = CGImageCreateWithMask([image CGImage], mask); 
    return [UIImage imageWithCGImage:masked]; 

} 
+0

यह स्विफ्ट 4 पर मेरे लिए काम नहीं करता है और मुझे मूल छवि को मुखौटा छवि के रूप में मिलता है। – iAviator

संबंधित मुद्दे