2010-03-04 11 views
15

मेरे पास ऑब्जेक्ट्स का संग्रह है जो छवि-नाम, इसका आकार और इसका एक्स/वाई स्थान का वर्णन करता है। संग्रह "परतों" द्वारा क्रमबद्ध किया गया है, इसलिए मैं छवियों को एक प्रकार के चित्रकार के एल्गोरिदम में मिश्रित कर सकता हूं।कई एनएसआईमेज को एक बड़ी छवि में कैसे मिश्रित करें?

इस से, मैं आयत छवियों के सभी पकड़ करने के लिए आवश्यक निर्धारित कर सकते हैं, इसलिए अब मुझे क्या करना चाहते हैं:

  • बफर के कुछ प्रकार (के एन एस बराबर बनाएं परिणाम धारण करने के लिए क्या आईफोनओएस UIGraphicsContext को कॉल करता है।)
  • सभी छवियों को बफर में खींचें।
  • बफर के मिश्रित परिणाम से एक नया एनएसआईमेज स्नैग करें।

iPhoneOS में, इस कोड है कि मैं क्या चाहते है:

UIGraphicsBeginImageContext (woSize); 
    CGContextRef ctx = UIGraphicsGetCurrentContext(); 
    [[UIColor clearColor] set]; 
    CGContextFillRect(ctx, NSMakeRect(0, 0, woSize.width, woSize.height)); 
    // draw my various images, here. 
    // i.e. Various repetitions of [myImage drawAtPoint:somePoint]; 
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext(); 
UIGraphicsEndImageContext(); 

क्या मैं के लिए देख रहा हूँ कैसे करना है कि डेस्कटॉप कोको/एन एस में।

धन्यवाद!

उत्तर

19
NSImage* resultImage = [[[NSImage alloc] initWithSize:imageSize] autorelease]; 
[resultImage lockFocus]; 

[anotherImage drawAtPoint:aPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1.0]; 
// Or any of the other about 6 options; see Apple's guide to pick. 

[resultImage unlockFocus]; 

एक बहुत लंबे समय तक, और अधिक विस्तृत जवाब के लिए चेक Apple's Drawing Guide

+1

डॉक्स का कहना है कि हिम-तेंदुए में गिरावट आई है। "महत्वपूर्ण: यदि आप नया कोड लिख रहे हैं, या पुराने कोड को अपडेट कर रहे हैं, तो आपको इस विधि का उपयोग करने से बचना चाहिए। इसके बजाय, आपको drawAtPoint का उपयोग करना चाहिए: सेक्ट: ऑपरेशन: अंश: या drawInRect: fromRect: operation: fraction: draw to method छवि। हालांकि विधि स्वयं को बहिष्कृत नहीं किया गया है, लेकिन सामान्य व्यवहार के लिए यह व्यवहार प्रदान नहीं किया जाता है। " लेकिन मैं drawAtPoint आज़माउंगा, और यदि यह काम करता है, तो आपको सही उत्तर चेकमार्क दें। (आप अपना उत्तर संपादित करना चाहेंगे।) धन्यवाद! – Olie

+0

अच्छी कॉल। संपादित। कारण, मुझे लगता है कि ड्रॉ स्रोत स्रोत की जांच करता है और समग्र नहीं करता है। यह तब तक ठीक है जब तक आप सुनिश्चित हों कि आप एक समझदार आयत पार कर रहे हैं (और यह थोड़ा तेज़ भी है) लेकिन ड्रॉ का उपयोग करना वास्तव में सुरक्षित है। – andyvn22

+0

हाँ, लॉकफोकस वह चीज़ थी जिसे मैं याद कर रहा था (इस तरह का एक सहज नाम, मैं कल्पना नहीं कर सकता कि मैंने इसके लिए दस्तावेज़ों की जांच क्यों नहीं की थी!;) धन्यवाद! – Olie

0
#import <Cocoa/Cocoa.h> 

@interface CompositeView : NSView { 
    NSImage *bottom; 
    NSImage *top; 
} 
- (IBAction)takeBottomFrom: (id)aView; 
- (IBAction)takeTopFrom: (id)aView; 
@end 

#import "CompositeView.h" 

@implementation CompositeView 
- (IBAction)takeBottomFrom: (id)aView 
{ 
    id img = [[aView image] retain]; 
    [bottom release]; 
    bottom = img; 
    [self setNeedsDisplay: YES]; 
} 

- (IBAction)takeTopFrom: (id)aView 
{ 
    id img = [[aView image] retain]; 
    [top release]; 
    top = img; 
    [self setNeedsDisplay: YES]; 
} 

- (void)drawRect:(NSRect)rect 
{ 
    NSCompositingOperation op = 0; 
    NSRect bounds = [self bounds]; 
    NSSize imageSize = bounds.size; 
    imageSize.width /= 7; 
    imageSize.height /= 2; 

    NSRect bottomRect = { {0,0}, [bottom size] }; 
    NSRect topRect = { {0,0}, [top size] }; 

    for (unsigned y=0 ; y<2 ; y++) 
    { 
     for (unsigned x=0 ; x<7 ; x++) 
     { 
      NSRect drawRect; 

      drawRect.origin.y = y * imageSize.height; 
      drawRect.origin.x = x * imageSize.width; 
      drawRect.size = imageSize; 

      [bottom drawInRect: drawRect 
         fromRect: bottomRect 
        operation: NSCompositeCopy 
         fraction: 1]; 

      [top drawInRect: drawRect 
        fromRect: topRect 
        operation: op++ 
        fraction: 1]; 
     } 
    } 
} 

- (id)initWithFrame:(NSRect)frame { 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // Initialization code here. 
    } 
    return self; 
} 

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