खोज…


टिप्पणियों

UIImage के लिए Apple डेवलपर विषय

UIImage बनाना

स्थानीय छवि के साथ

तीव्र

let image = UIImage(named: "imageFromBundleOrAsset")

उद्देश्य सी

UIImage *image = [UIImage imageNamed:@"imageFromBundleOrAsset"];

ध्यान दें

विधि की छवि imageNamed छवि की सामग्री को मेमोरी में कैश करती है। कई बड़ी छवियों को लोड करने के तरीके से कम मेमोरी चेतावनी हो सकती है जो एप्लिकेशन को समाप्त कर सकती है। यह UIImage की विधि imageWithContentsOfFile का उपयोग करके तय किया जा सकता है, जो कैशिंग का उपयोग नहीं करता है।

NSData के साथ

तीव्र

let imageData = Data(base64Encoded: imageString, options: Data.Base64DecodingOptions.ignoreUnknownCharacters)

let image = UIImage(data: imageData!)

UIColor के साथ

तीव्र

let color = UIColor.red
let size = CGSize(width: 200, height: 200)
    
UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
UIGraphicsGetCurrentContext()!.setFillColor(color.cgColor)
UIGraphicsGetCurrentContext()!.fill(CGRect(origin: .zero, size: size))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

उद्देश्य सी

UIColor *color=[UIColor redColor];
CGRect frame = CGRectMake(0, 0, 80, 100);
UIGraphicsBeginImageContext(frame.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, frame);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

फ़ाइल सामग्री के साथ

उद्देश्य सी

उदाहरण:

UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellCountry objectForKey:@"Country_Flag"] ofType:nil]];

सरणी का उपयोग करना:

उदाहरण:

NSMutableArray *imageArray = [[NSMutableArray alloc] init];

for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", self.myPhoto];

    // check if a file exists
    if ([UIImage imageNamed:fileName]) {
        // if it exists, add it to the array
        [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@", fileName] ofType:@""]]];
    } else {
        break;
    }
}

// यहाँ एनिमेशन के लिए छवि सरणी का उपयोग:

self.myImageView.animationImages = imageArray;

फ़ाइल सामग्री के साथ छवि ऑब्जेक्ट बनाना और प्रारंभ करना

निर्दिष्ट पथ पर फ़ाइल से छवि डेटा लोड करके एक छवि ऑब्जेक्ट बनाना और वापस करना।

उदाहरण:

 UIImage *image = [UIImage imageWithContentsOfFile:[[NSBundle mainBundle] pathForResource:[cellCountry objectForKey:@"Country_Flag"] ofType:nil]];

सरणी का उपयोग करना:

उदाहरण

 NSMutableArray *imageArray = [[NSMutableArray alloc] init];

for (int imageNumber = 1; self.myPhoto != nil; imageNumber++) {
    NSString *fileName = [NSString stringWithFormat:@"%@.jpg", self.myPhoto];

    // check if a file exists
    if ([UIImage imageNamed:fileName]) {
        // if it exists, add it to the array
        [imageArray addObject:[UIImage imageWithContentsOfFile:[[NSBundle mainBundle]pathForResource:[NSString stringWithFormat:@"%@", fileName] ofType:@""]]];
    } else {
        break;
    }
}

//Using image array for animations here
self.myImageView.animationImages = imageArray;

कैप्स के साथ आकार बदलने योग्य छवि

नीचे दिए गए एक संदेश बबल के उदाहरण में: छवि के कोनों को अपरिवर्तित रहना चाहिए जो UIEdgeInsets द्वारा निर्दिष्ट किया UIEdgeInsets , लेकिन नए आकार को कवर करने के लिए छवि की सीमाओं और केंद्र का विस्तार होना चाहिए।

मूल छवि

आकार बदला हुआ चित्र

 let insets = UIEdgeInsetsMake(12.0, 20.0, 22.0, 12.0)
 let image = UIImage(named: "test")
 image?.resizableImageWithCapInsets(insets, resizingMode: .Stretch)

छवियों की तुलना

isEqual: विधि यह निर्धारित करने का एकमात्र विश्वसनीय तरीका है कि क्या दो छवियों में समान छवि डेटा है। आपके द्वारा बनाई गई छवि ऑब्जेक्ट एक-दूसरे से भिन्न हो सकते हैं, तब भी जब आप उन्हें एक ही कैश्ड छवि डेटा के साथ आरंभ करते हैं। उनकी समानता का निर्धारण करने का एकमात्र तरीका है isEqual: विधि का उपयोग करना, जो वास्तविक छवि डेटा की तुलना करता है। लिस्टिंग 1 छवियों की तुलना करने के लिए सही और गलत तरीके दिखाता है।

स्रोत: Apple प्रलेखन

तीव्र

// Load the same image twice.
let image1 = UIImage(named: "MyImage")
let image2 = UIImage(named: "MyImage")

// The image objects may be different, but the contents are still equal
if let image1 = image1, image1.isEqual(image2) {
    // Correct. This technique compares the image data correctly.
}

if image1 == image2 {
    // Incorrect! Direct object comparisons may not work.
}

उद्देश्य सी

// Load the same image twice.
UIImage* image1 = [UIImage imageNamed:@"MyImage"];
UIImage* image2 = [UIImage imageNamed:@"MyImage"];
 
// The image objects may be different, but the contents are still equal
if ([image1 isEqual:image2]) {
   // Correct. This technique compares the image data correctly.
}
 
if (image1 == image2) {
   // Incorrect! Direct object comparisons may not work.
}

UIColor के साथ UIImage बनाएं

तीव्र

let color = UIColor.redColor()
let size = CGSize(width: 200, height: 200)

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(), color.CGColor)
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRect(origin: .zero, size: size))
let colorImage = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()

स्विफ्ट 3

let color = UIColor.red()
let size = CGSize(width: 200, height: 200)

UIGraphicsBeginImageContextWithOptions(size, false, 0.0)
if let context = UIGraphicsGetCurrentContext() {
    context.setFillColor(color.cgColor)
    context.fill(CGRect(origin: .zero, size: size))
    let colorImage = UIGraphicsGetImageFromCurrentImageContext()
}
UIGraphicsEndImageContext()

उद्देश्य सी:

इस विधि को UIImage विस्तार के रूप में जोड़ें:

+ (UIImage *)createImageWithColor: (UIColor *)color {
    CGRect rect=CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
    UIGraphicsBeginImageContext(rect.size);
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetFillColorWithColor(context, [color CGColor]);
    CGContextFillRect(context, rect);

    UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return theImage;
}

रंगों के साथ ढाल छवि

CGRect में रंगों के साथ ग्रेडिएंट UIImage बनाना

स्विफ्ट:

extension UIImage {
    static func gradientImageWithBounds(bounds: CGRect, colors: [CGColor]) -> UIImage {
        let gradientLayer = CAGradientLayer()
        gradientLayer.frame = bounds
        gradientLayer.colors = colors
        
        UIGraphicsBeginImageContext(gradientLayer.bounds.size)
        gradientLayer.render(in: UIGraphicsGetCurrentContext()!)
        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

उपयोग:

let image = UIImage.gradientImageWithBounds(CGRect(x: 0, y: 0, width: 200, height: 200), colors: [UIColor.yellowColor().CGColor, UIColor.blueColor().CGColor])

उद्देश्य सी:

+ (UIImage *)gradientImageWithBounds:(CGRect)bounds colors:(NSArray *)colors {
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    gradientLayer.frame = bounds;
    gradientLayer.colors = colors;
    
    UIGraphicsBeginImageContext(gradientLayer.bounds.size);
    [gradientLayer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
}

सीमा के लिए ग्रेडिएंट बैकग्राउंड लेयर

+ (CALayer *)gradientBGLayerForBounds:(CGRect)bounds colors:(NSArray *)colors
{
    CAGradientLayer * gradientBG = [CAGradientLayer layer];
    gradientBG.frame = bounds;
    gradientBG.colors = colors;
    return gradientBG;
}

कनवर्ट करें UIImage / से base64 एन्कोडिंग

एन्कोडिंग

//convert the image to NSData first
let imageData:NSData = UIImagePNGRepresentation(image)!
// convert the NSData to base64 encoding
let strBase64:String = imageData.base64EncodedStringWithOptions(.Encoding64CharacterLineLength)

डिकोडिंग

let dataDecoded:NSData = NSData(base64EncodedString: strBase64, options: NSDataBase64DecodingOptions(rawValue: 0))!
let decodedimage:UIImage = UIImage(data: dataDecoded)!

एक UIView का स्नैपशॉट लें

//Here self.webView is the view whose screenshot I need to take
//The screenshot is saved in jpg format in the application directory to avoid any loss of quality in retina display devices i.e. all current devices running iOS 10    
UIGraphicsBeginImageContextWithOptions(self.webView.bounds.size, NO, [UIScreen mainScreen].scale);
[self.webView.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSString  *jpgPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/Test.jpg"];
[UIImageJPEGRepresentation(image, 1.0) writeToFile:jpgPath atomically:YES];
UIImage *pop=[[UIImage alloc]initWithContentsOfFile:jpgPath];
//pop is the final image in jpg format and high quality with the exact resolution of the view you selected in pixels and not just points  

UIImage को UIColor लागू करें

निम्न के रूप में केवल UIImage उदाहरण के लिए UIColor लागू करके कई थीम बेस ऐप के साथ एक ही UIImage का उपयोग करें।

// *** Create an UIImage instance with RenderingMode AlwaysTemplate ***
UIImage *imgMenu = [[UIImage imageNamed:@"iconMenu"] imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];

// *** Now Apply `tintColor` to `UIImageView` of UIImageView or UIButton and convert image in given color ***
[btn setImage:imgMenu forState:UIControlStateNormal]; // Set UIImage in UIButton.

[button.imageView setTintColor:[UIColor blueColor]]; // It changes image color of UIButton to blue color

अब आप UIImageView के साथ भी ऐसा ही करना चाहते हैं तो निम्न कोड का उपयोग करें

[imageView setImage:imgMenu]; // Assign UIImage to UIImageView
[imageView setTintColor:[UIColor greenColor]]; // Change imageview image color to green.
[imageView setTintColor:[UIColor redColor]]; // Change imageview image color to red.

UIImage रंग बदलें

स्विफ्ट इस एक्सटेंशन को UIImage में जोड़ें:

extension UIImage {
    func maskWithColor(color: UIColor) -> UIImage? {
    
        let maskImage = self.CGImage
        let width = self.size.width
        let height = self.size.height
        let bounds = CGRectMake(0, 0, width, height)
    
        let colorSpace = CGColorSpaceCreateDeviceRGB()
        let bitmapInfo = CGBitmapInfo(rawValue: CGImageAlphaInfo.PremultipliedLast.rawValue)
        let bitmapContext = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, colorSpace, bitmapInfo.rawValue) //needs rawValue of bitmapInfo
    
        CGContextClipToMask(bitmapContext, bounds, maskImage)
        CGContextSetFillColorWithColor(bitmapContext, color.CGColor)
        CGContextFillRect(bitmapContext, bounds)
    
        //is it nil?
        if let cImage = CGBitmapContextCreateImage(bitmapContext) {
            let coloredImage = UIImage(CGImage: cImage)
        
            return coloredImage
        
        } else {
            return nil
        }
    }
}

फिर, अपने UIImage का रंग बदलने के लिए

my_image.maskWithColor(UIColor.blueColor())

इस लिंक पर मिला



Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow