سؤال

لدي برنامج OBJ C بسيط، في الوقت الحالي، يتيح لك تحميل صورة، ويقسمها، ومن الناحية النظرية يجب أن تتيح لك التكبير وتدويرها. أنا أستخدم NSAffintranslations.

أريد أن تكون الصورة التي يجب إغلاقها على الجزء العلوي الأيسر (على عكس معيار PS / PDF ذي اليسار السفلي)، لذلك أنا أستخدم isflipped، والاتصال [aftrans scalexby: 1.0 yby: -1.0]؛

المشكلة هي أنه لسبب ما، بعد المرة الأولى التي يتم فيها استدعاء انتشار بلدي، لا يحدث التحول.

عندما أقوم بتحميل صورة، فهو يأتي، وتبدو صحيحة. إذا قمت بتغيير حجم النافذة (الذي يدعو إلى انتشار)، فإن الصورة تعادل، ولكن رأسا على عقب وعكسها. هذا يعني أن التحول لم يصبح ساري المفعول. لا أرى أي اختلافات في أي من البيانات المرة الثانية من خلال.

هنا نسخة تجريدية من الرمز:

- (void)drawRect:(NSRect)rect 
{
    // Drawing code here.

//    NSLog(@"window type: %d", [[self window] backingType]);
   NSAffineTransform *afTrans = [[NSAffineTransform alloc] init];
   NSGraphicsContext *context = [NSGraphicsContext currentContext];
   NSSize sz;
   NSRect windowFrame = [[self window] frame];
   NSRect cv =[[[self window] contentView] frame];
   float deltaX, deltaY;
   NSSize superSize = [[self superview] frame].size;
   float height, width, sHeight, sWidth;

   NSRect imageRect;

   sz = [ image size];
   imageRect.size = sz;
   imageRect.origin = NSZeroPoint;

   height = sz.height  ;
   width = sz.width  ;

//    sHeight and sWidth are the hieght and with of the super-view. ie,
//    the size of the whole window view including the space for the
//    scroll bars, etc, but not including the panel or the borders,
   sHeight = superSize.height;
   sWidth = superSize.width;

   [context saveGraphicsState];

   deltaX = 0;
   deltaY = 0;

   deltaY += height; // account for flipping

   [afTrans translateXBy:deltaX yBy:deltaY];

   [afTrans scaleXBy:1.0 yBy:-1.0];

   [afTrans concat];

   NSRect drawingRect = imageRect;
   NSRect frame = imageRect;
   [self setFrame:frame];

   [image drawInRect:drawingRect
         fromRect:imageRect
         operation:NSCompositeSourceOver
         fraction:1];

   [afTrans release];
   [context restoreGraphicsState];
}

ETA: إليك بعض التعليمات البرمجية الأخرى التي قد تكون ذات صلة.

-(void )setImage:( NSImage * )newImage
{
    [newImage retain];
    [image release];

    rotation = 0;

    zoom = 1.0;

    image = newImage;
    NSSize imageSize = [newImage size];
    NSRect tFrame = [self frame];
    tFrame = [[self window] frame];

    tFrame.size.width = MAX(tFrame.size.width, imageSize.width);
    tFrame.size.height = MAX(tFrame.size.height, imageSize.height);
    [self setFrame:tFrame];

    [self setNeedsDisplay:YES];
}
هل كانت مفيدة؟

المحلول

لست متأكدا تماما ما تحاول تحقيقه، ولكن إذا كنت ترغب في رسم صورة فقط في NSView والحفاظ عليها في الجزء العلوي الأيسر، فيمكنك القيام بشيء أكثر بساطة قليلا، في Subclass NSView الخاص بك:

- (BOOL)isFlipped
{
    return YES;
}

- (void)setImage:(NSImage *)newImage
{
    [newImage retain];
    [image release];
    image = newImage;

    [image setFlipped:YES];
    [self setNeedsDisplay:YES];
}

- (void)drawRect:(NSRect)rect
{
    [image drawAtPoint:NSZeroPoint fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];

    // Or stretch image to fill view
    //[image drawInRect:[self bounds] fromRect:NSZeroRect operation:NSCompositeSourceOver fraction:1];
}

كنت أيضا قادرا على الحصول على نتائج مماثلة مع التعليمات البرمجية الخاصة بك عن طريق التخلص من [self setFrame:...]; يدعو بطرق ورصة وطرائق SETIMAGE.

نصائح أخرى

على افتراض أنك لا تجريد الكود الذي يغير الأشياء، احترس إذا كنت ترسم الصورة في مكان آخر. قد يؤدي عناصر شريط الأدوات، الجداول وما إلى ذلك إلى تغيير خاصية ZEMPUMPED للصورة، مما تسبب في رسمها بشكل غير صحيح.

جرب هذا حول خط رسم صورتك:

BOOL wasFlipped = [image isFlipped];
[image setFlipped:[self isFlipped]];
[image drawInRect:drawingRect
             fromRect:imageRect
            operation:NSCompositeSourceOver
             fraction:1];
[image setFlipped:wasFlipped];

معرفة ما إذا كان ذلك يساعد. لو أنه هل, ، ابحث في مكان آخر في التعليمات البرمجية الخاصة بك لشيء تغيير الممتلكات المنطقية للصورة وعدم وضعه مرة أخرى.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top