Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How do I pattern an image in a view w/o "scrolling"?



I screwed up one of the rects in the second block of code. It should be:

//--------------------------------------------
// draw image data into a new NSImage, tiling a 10px-wide image
horizontally

NSRect imageTileRect = NSMakeRect(0, 0, 1, [yourOtherImageData
size].height);
NSSize aSize = NSMakeSize(128, 128);
NSRect newImageRect = NSMakeRect(0, 0, aSize.width, aSize.height);
NSImage *newImage = [[NSImage alloc] initWithSize:aSize];

[newImage lockFocus];
[yourOtherImageData drawInRect:newImageRect fromRect:imageTileRect
operation:NSCompositeSourceAtop fraction:1.0];
[newImage unlockFocus];

//--------------------------------------------

The newImage is 128x128, and the tile image that gets repeated is 10x128. I must've sneezed and gotten confused as to what I was doing.



On 27 Jul, 2006, at 20:18, Michael Watson wrote:

On 27 Jul, 2006, at 19:40, Michael Watson wrote:

The answer to this is a bit lateral:

Let's say we have some image data in memory that is 128x128. We want to draw it into an NSImage that is 128x128. We can do something like this:

//--------------------------------------------
// draw image data into a new NSImage, without any tiling

NSSize aSize = NSMakeSize(128, 128);
NSRect newImageRect = NSMakeRect(0, 0, aSize.width, aSize.height)
NSImage *newImage = [[NSImage alloc] initWithSize:aSize];

[newImage lockFocus];
[yourOtherImageData drawInRect:newImageRect fromRect:newImageRect operation:NSCompositeSourceAtop fraction:1.0];
[newImage unlockFocus];


//--------------------------------------------

Let's say, though, that we have a 10x128 image that we want to tile into a 128x128 square. If we're using NSImage, we can do this with NSImage to accomplish that:

//--------------------------------------------
// draw image data into a new NSImage, tiling a 10px-wide image horizontally


NSRect imageTileRect = NSMakeRect(0, 0, 1, [yourOtherImageData size].height)
NSSize aSize = NSMakeSize(10, 128);
NSRect newImageRect = NSMakeRect(0, 0, aSize.width, aSize.height)
NSImage *newImage = [[NSImage alloc] initWithSize:aSize];


[newImage lockFocus];
[yourOtherImageData drawInRect:newImageRect fromRect:imageTileRect operation:NSCompositeSourceAtop fraction:1.0];
[newImage unlockFocus];


//--------------------------------------------


What happens is that you're telling newImage to fill its entire rect with an image smaller than the newImageRect dimensions, so NSImage automatically tiles the drawn image.



Note: Untested code, written in e-mail, may contain typos or other errors. But you get the idea.


--
Michael Watson

On 27 Jul, 2006, at 18:16, Alan Smith wrote:

It would help if I knew how to "tile" an NSImage. In your explanation
it seems that I just make an NSImage with a 1px wide image and I'm all
set. Well, I need the image to repeated over and over again in one
direction, creating a "bar." I can't figure out how to do this with
NSImage.


Thanks for being so patient, Alan


--
// Quotes from yours truly -------------------------
"You don't forget, you just don't remember."
"Maturity resides in the mind."
"Silence is the Universe's greatest gift."
"Don't waste your life doing things others have already done."
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/mikey-san% 40bungie.org


This email sent to email@hidden



_______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/cocoa-dev/email@hidden

This email sent to email@hidden
References: 
 >How do I pattern an image in a view w/o "scrolling"? (From: "Alan Smith" <email@hidden>)
 >Re: How do I pattern an image in a view w/o "scrolling"? (From: "I. Savant" <email@hidden>)
 >Re: How do I pattern an image in a view w/o "scrolling"? (From: "Alan Smith" <email@hidden>)
 >Re: How do I pattern an image in a view w/o "scrolling"? (From: "I. Savant" <email@hidden>)
 >Re: How do I pattern an image in a view w/o "scrolling"? (From: "Alan Smith" <email@hidden>)
 >Re: How do I pattern an image in a view w/o "scrolling"? (From: Michael Watson <email@hidden>)
 >Re: How do I pattern an image in a view w/o "scrolling"? (From: Michael Watson <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.