[futurebasic] Incorporating images using FBtoC

Message: < previous - next > : Reply : Subscribe : Cleanse
Home   : November 2012 : Group Archive : Group : All Groups

From: Ken Shmidheiser <kshmidheiser@...>
Date: Fri, 2 Nov 2012 01:00:49 -0400
Stephanie,

No need for .RSRC files anymore. Simply drop your images into 
the same folder as your FB source code and FB will do the heavy
lifting.

Please note there are more modern ways to do this, but they require
a knowledge of HIImageViews or, better yet, Cocoa.

Ken


/*
  Add your JPEG or PNG image in the some folder as your FB souce code
  In this case our image is a JPEG called "MyImage,jpg"
*/
resources "MyImage.jpg"


// This header gives you access to CoreGraphics toolox functions
include "Tlbx CoreGraphics.incl"


// Use this function to create the CGImageRef of a JPEG or PNG image added as a resource file
local fn CreateCGImageFromBundleResource( fileName as CFStringRef ) as CGImageRef
'~'1
dim as CFURLRef           url
dim as CGImageRef         imageRef : imageRef = 0
dim as CGDataProviderRef  dataProvider

url = fn CFBundleCopyResourceURL( fn CFBundleGetMainBundle(), fileName, 0, 0 )
long if ( url )
dataProvider = fn CGDataProviderCreateWithURL( url )
long if ( dataProvider )
select
case fn CFStringHasSuffix( fileName, fn CFSTR( ".jpg" ) )
imageRef = fn CGImageCreateWithJPEGDataProvider( dataProvider, NULL, _false, _kCGRenderingIntentDefault )
case fn CFStringHasSuffix( fileName, fn CFSTR( ".png" ) )
imageRef = fn CGImageCreateWithPNGDataProvider( dataProvider, NULL, _false, _kCGRenderingIntentDefault )
end select
fn CGDataProviderRelease( dataProvider )
end if
CFRelease( url )
end if
end fn = imageRef


dim as rect r
dim as ControlButtonContentInfo content

SetRect ( @r, 0, 0, 600, 600 )
appearance window 1,  "",  @r,  _kDocumentWindowClass
fn SetWindowBackground(  _kThemeActiveDialogBackgroundBrush,  _true )

// Tweak you ImageWell boundaries as needed
SetRect ( @r, 10, 10, 580, 580 )
appearance button 1,,,,, "", @r , _kControlImageWellProc

content.contentType = _kControlContentCGImageRef
// Note: Be sure to use the "@" prefix before the image name.
content.u.imageRef = fn CreateCGImageFromBundleResource( @"MyImage.jpg" )

fn SetButtonData( 1, _kControlImageWellPart, _kControlImageWellContentTag, SizeOf( content ), content )

do
HandleEvents
until gFBQuit