Pictures can be restored in two different ways:
the picture header can be restored, and the picture data restored immediately after
the picture header can be restored, and the loading of the picture data deferred until a later time
This second case is commonly used when loading rich text documents that include pictures. Using a deferred loading scheme, pictures that occur towards the end of a document need not be loaded until they are needed to be displayed, thus economising on memory use.
This fragment assumes that the picture header is stored in the root stream of the store, making it easy to restore:
// The file used in the example
_LIT(KFileName,"C:\\grpict.dat");
// Open the direct file store
CDirectFileStore* store = CDirectFileStore::OpenLC(fsSession,KFileName,EFileRead);
// Read the header stream
RStoreReadStream stream;
stream.OpenLC(*store,store->Root());
TPictureHeader header;
header.InternalizeL(stream);
// Close store
CleanupStack::PopAndDestroy(2); // stream, store
Open the direct file store containing the picture.
Open the root stream of the store, and internalise the picture header.
Close the stream and store.
To restore the picture data itself, it must have an associated entry in
a picture factory. The picture factory ensures that the picture data in the
store is restored to the correct picture type, which is specified by a UID in
the picture header. A picture factory can allow a number of different types of
picture to be restored, calling the correct constructor for the class indicated
by the UID. In the following example code the picture factory allows only
pictures of type CSmileyPicture
to be restored, and panics if the
picture data in the store is of another type.
// The file used in the example
_LIT(KFileName,"C:\\grpict.dat");
// Open the direct file store
CDirectFileStore* store = CDirectFileStore::OpenLC(fsSession,KFileName,EFileRead);
// Read the picture
TExamplePictureFactory factory;
TPictureHeader header;
factory.NewPictureL(header,*store);
iPicture = (CSmileyPicture *) header.iPicture.AsPtr();
// Close store
CleanupStack::PopAndDestroy();
class TExamplePictureFactory: public MPictureFactory
{
public:
void NewPictureL(TPictureHeader& aHeader,
const CStreamStore& aDeferredPictureStore) const;
};
void TExamplePictureFactory::NewPictureL(TPictureHeader& aHeader,
const CStreamStore& aDeferredPictureStore) const
{
if (aHeader.iPictureType == KUidExampleSmileyPicture)
{
// Restore new picture from store into
// the TSwizzle, which changes from
// stream id to pointer.
// Construct CSmileyPicture object and
// restore from stream.
if (aHeader.iPicture.IsId())
aHeader.iPicture = CSmileyPicture::NewL(aDeferredPictureStore,aHeader.iPicture.AsId());
}
else
{
// Leave
User::Leave(KErrNoMemory);
}
}