2012-07-09 9 views
7

मैं AFMetworking लाइब्रेरी से setImageWithUrl का उपयोग कर तालिका कक्षों में UImageViews सेट कर रहा हूं, लेकिन मुझे छवियों को ग्रेस्केल होने की आवश्यकता है ... क्या मैं ऐसा कर सकता हूं। मैंने कुछ यूआईएममेज ग्रेस्केल कन्वर्टर्स की कोशिश की है, लेकिन मुझे लगता है कि वे काम नहीं करते हैं क्योंकि मैं ऐसा कुछ सेट कर रहा हूं जो अभी तक डाउनलोड नहीं हुआ है। मैं इसे यहाँ पायाUIImageView ग्रे बनाना

- (UIImage *)convertImageToGrayScale:(UIImage *)image 
{ 
    // Create image rectangle with current image width/height 
    CGRect imageRect = CGRectMake(0, 0, image.size.width, image.size.height); 

    // Grayscale color space 
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); 

    // Create bitmap content with current image size and grayscale colorspace 
    CGContextRef context = CGBitmapContextCreate(nil, image.size.width, image.size.height, 8, 0, colorSpace, kCGImageAlphaNone); 

    // Draw image into current context, with specified rectangle 
    // using previously defined context (with grayscale colorspace) 
    CGContextDrawImage(context, imageRect, [image CGImage]); 

    // Create bitmap image info from pixel data in current context 
    CGImageRef imageRef = CGBitmapContextCreateImage(context); 

    // Create a new UIImage object 
    UIImage *newImage = [UIImage imageWithCGImage:imageRef]; 

    // Release colorspace, context and bitmap information 
    CGColorSpaceRelease(colorSpace); 
    CGContextRelease(context); 
    CFRelease(imageRef); 

    // Return the new grayscale image 
    return newImage; 
} 

:

+1

[का प्रयास इस तरह की एक मुख्य छवि फ़िल्टर] [1] [1]: http://stackoverflow.com/questions/10030631/what-is-the-best-core- छवि-फ़िल्टर-टू-उपज-काले-और-सफेद प्रभाव –

उत्तर

14

इस विधि की कोशिश करो एक UIImageView के बजाय http://mobiledevelopertips.com/graphics/convert-an-image-uiimage-to-grayscale.html

+1

यह मेरे लिए बहुत अच्छा काम करता है! मैंने कोर छवि फ़िल्टर की कोशिश की और प्रदर्शन मेरे संग्रह दृश्य में खराब था। इस विधि का उपयोग करना बहुत तेज़ है। अच्छी तरह से किया –

+1

यह केवल तभी काम करता है जब आप पारदर्शिता का उपयोग नहीं करते हैं (अल्फा किसी को भी सेट नहीं किया गया है)। –

1

, एक UIView उपवर्ग, दे यह

@property (strong, nonatomic) UIImage *image; 

@synthesize image = _image; 
साथ

ओवरराइड सेटर

-(void)setImage:(UIImage *)image{ 
    _image = [self makeImageBW:image]; 
    [self setNeedsDisplay]; 
} 

- (UIImage *)makeImageBW:(UIImage *)source 
{ 
    CIImage *beginImage = source.CIImage; 
    CIImage *blackAndWhite = [CIFilter filterWithName:@"CIColorControls" keysAndValues:kCIInputImageKey, beginImage, @"inputBrightness", [NSNumber numberWithFloat:0.0], @"inputContrast", [NSNumber numberWithFloat:1.1], @"inputSaturation", [NSNumber numberWithFloat:0.0], nil].outputImage; 
    CIImage *output = [CIFilter filterWithName:@"CIExposureAdjust" keysAndValues:kCIInputImageKey, blackAndWhite, @"inputEV", [NSNumber numberWithFloat:0.7], nil].outputImage; 
    CIContext *context = [CIContext contextWithOptions:nil]; 
    CGImageRef ref = [context createCGImage:output fromRect:output.extent]; 
    UIImage *newImage = [UIImage imageWithCGImage:ref]; 
    CGImageRelease(cgiimage); 
    return newImage; 
} 

-(void)drawRect:(CGRect)rect{ 
    [_image drawInRect:rect]; 
} 

कहीं और आप अपने NSURLRequest के साथ छवि सेट कर सकते हैं:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^{ 
     NSData *data = [NSURLConnection sendSynchronousRequest:(NSURLRequest *) returningResponse:nil error:nil]; 
     if (data){ 
      UIImage *image = [UIImage imageWithData:data]; 
      if (image) dispatch_async(dispatch_get_main_queue(),^{ 
       [view setImage:image]; 
      }); 
     } 
}); 
+0

मुझे लगता है कि इसे एक UIView बनाने से मुझे AFNetworking की setImageWithUrl विधि का उपयोग करने की अनुमति नहीं होगी ..? – cannyboy

+0

मैं एएफनेटवर्किंग से परिचित नहीं हूं, मैंने कोड जोड़ा है, इसलिए यह एएफ लाइब्रेरी –

+0

व्यक्तिगत रूप से काम करेगा, मैं इसे 'यूआईएममेज' पर एक श्रेणी के रूप में जोड़ूंगा: '- [यूआईएममेज (ग्रीस्केल) ग्रेस्केल इमेज] 'तो आप कुछ भी subclass करने की जरूरत नहीं है। – nielsbot

3

मैं @Jesse Gumpo के उदाहरण से कोड ले लिया, इसके बाद के संस्करण, लेकिन यहाँ यह एक अंतरफलक के रूप है।

@implementation UIImage (Greyscale) 

- (UIImage *)greyscaleImage 
{ 
    CIImage * beginImage = [ self CIImage ] ; 
    CIImage * evAdjustedCIImage = nil ; 
    { 
     CIFilter * filter = [ CIFilter filterWithName:@"CIColorControls" 
             keysAndValues:kCIInputImageKey, beginImage 
              , @"inputBrightness", @0.0 
              , @"inputContrast", @1.1 
              , @"inputSaturation", @0.0 
              , nil ] ; 
     evAdjustedCIImage = [ filter outputImage ] ; 
    } 

    CIImage * resultCIImage = nil ; 
    { 
     CIFilter * filter = [ CIFilter filterWithName:@"CIExposureAdjust" 
             keysAndValues:kCIInputImageKey, evAdjustedCIImage 
              , @"inputEV", @0.7 
              , nil ] ; 
     resultCIImage = [ filter outputImage ] ; 
    } 

    CIContext * context = [ CIContext contextWithOptions:nil ] ; 
    CGImageRef resultCGImage = [ context createCGImage:resultCIImage 
               fromRect:resultCIImage.extent ] ; 
    UIImage * result = [ UIImage imageWithCGImage:resultCGImage ] ; 
    CGImageRelease(resultCGImage) ; 

    return result; 
} 

@end 

अब आप यह कर सकते हैं:

UIImage * downloadedImage = ... get from AFNetwork results ... ; 
downloadedImage = [ downloadedImage greyscaleImage ] ; 

... use 'downloadedImage' ... 
+0

क्या आपको स्रोत में पास करने की आवश्यकता है यदि इसे यूआईएममेज पर बुलाया जा रहा है? निचला फ़ंक्शन कॉल सरल लगता है, लेकिन उपरोक्त इंटरफ़ेस इनपुट पैरामीटर लेता है, जो अनावश्यक लगता है। क्या होगा अगर इसे अभी कहा जाता है - (UIImage *) greyscaleImage? – Cindeselia

+0

हां - यह एक टाइपो था। धन्यवाद। – nielsbot

1

यह धागा थोड़ा पुराना है लेकिन मैं अपने खोज पर उसकी बारे में जाना। this post में मैंने एक ग्रेस्केल छवि बनाने के लिए स्विफ्ट में 2 अलग-अलग तरीकों की ओर इशारा किया है जिसे छवि में प्रदर्शित किया जा सकता है छवि के अल्फा और स्केल पर विचार करें।

import CoreImage 

extension UIImage 
{ 
    /// Applies grayscale with CIColorControls by settings saturation to 0.0. 
    /// - Parameter brightness: Default is 0.0. 
    /// - Parameter contrast: Default is 1.0. 
    /// - Returns: The grayscale image of self if available. 
    func grayscaleImage(brightness: Double = 0.0, contrast: Double = 1.0) -> UIImage? 
    { 
     if let ciImage = CoreImage.CIImage(image: self, options: nil) 
     { 
      let paramsColor: [String : AnyObject] = [ kCIInputBrightnessKey: NSNumber(double: brightness), 
                 kCIInputContrastKey: NSNumber(double: contrast), 
                 kCIInputSaturationKey: NSNumber(double: 0.0) ] 
      let grayscale = ciImage.imageByApplyingFilter("CIColorControls", withInputParameters: paramsColor) 

      let processedCGImage = CIContext().createCGImage(grayscale, fromRect: grayscale.extent) 
      return UIImage(CGImage: processedCGImage, scale: self.scale, orientation: self.imageOrientation) 
     } 
     return nil 
    } 

    /// Create a grayscale image with alpha channel. Is 5 times faster than grayscaleImage(). 
    /// - Returns: The grayscale image of self if available. 
    func convertToGrayScale() -> UIImage? 
    { 
     // Create image rectangle with current image width/height * scale 
     let pixelSize = CGSize(width: self.size.width * self.scale, height: self.size.height * self.scale) 
     let imageRect = CGRect(origin: CGPointZero, size: pixelSize) 
     // Grayscale color space 
     if let colorSpace: CGColorSpaceRef = CGColorSpaceCreateDeviceGray() 
     { 
      // Create bitmap content with current image size and grayscale colorspace 
      let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.None.rawValue) 
      if let context: CGContextRef = CGBitmapContextCreate(nil, Int(pixelSize.width), Int(pixelSize.height), 8, 0, colorSpace, bitmapInfo.rawValue) 
      { 
       // Draw image into current context, with specified rectangle 
       // using previously defined context (with grayscale colorspace) 
       CGContextDrawImage(context, imageRect, self.CGImage) 
       // Create bitmap image info from pixel data in current context 
       if let imageRef: CGImageRef = CGBitmapContextCreateImage(context) 
       { 
        let bitmapInfoAlphaOnly = CGBitmapInfo(rawValue: CGImageAlphaInfo.Only.rawValue) 
        if let contextAlpha = CGBitmapContextCreate(nil, Int(pixelSize.width), Int(pixelSize.height), 8, 0, nil, bitmapInfoAlphaOnly.rawValue) 
        { 
         CGContextDrawImage(contextAlpha, imageRect, self.CGImage) 
         if let mask: CGImageRef = CGBitmapContextCreateImage(contextAlpha) 
         { 
          // Create a new UIImage object 
          if let newCGImage = CGImageCreateWithMask(imageRef, mask) 
          { 
           // Return the new grayscale image 
           return UIImage(CGImage: newCGImage, scale: self.scale, orientation: self.imageOrientation) 
          } 
         } 
        } 
       } 
      } 

     } 
     // A required variable was unexpected nil 
     return nil 
    } 
} 
+0

यदि आप इस उत्तर को इस साइट पर रहने के लिए चाहते हैं तो आपको यहां अपनी पोस्ट का विवरण देना होगा। – Martin

+0

@ मार्टिन मैंने आपके लिए एक्सटेंशन कोड कॉपी किया है। – FBente