Wiki source for UIImageToJPeg


Show raw source

The Blob and Swirl apps let you select a UIImage from your device via the image picker, but then what if you want to feed to Squeak?
Squeak doesn't know what a UIImage is so


%%(language-ref)
- (NSData *) makeUIImageJPEGRepresentation: (UIImage*) image compression: (CGFloat) compress width: (CGFloat) width height: (CGFloat) height {
CGSize newSize;
newSize.width = width;
newSize.height = height;
UIGraphicsBeginImageContext( newSize );// a CGSize that has the size you want
[image drawInRect: CGRectMake(0,0,newSize.width,newSize.height)];
//image is the original UIImage
UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

NSData *data = UIImageJPEGRepresentation (newImage,compress);
[data retain];
return data;
}

%%
On the Smalltalk side anImage is the UIImage insance, we indicate the JPEG compression factor, and the width/height and make the call
We also have to later release the anImage, then we make the ObjectiveCBridge call to move the bytes from jpegNSData into a Smalltalk
byteArray instance and as you see free the jpegNSData NSdata instance.

stream then is a read stream that reads from the rawData which is the JPEG data, the form fromBinaryStream: figures out it's a jpeg file and reads it.

Obviously we could have a method that would directly create a Form instance from the UIImage but that would be lot's more work, yet very efficient.

%%(language-ref)
jpegNSData := sharedApplication delegate makeUIImageJPEGRepresentation: anImage compression: 8.0 width: (Display boundingBox width* 1.0) height: (Display boundingBox height* 1.0).
anImage release.
rawData := ObjectiveCBridge fetchByteArrayFrom: jpegNSData bytes length: jpegNSData length.
jpegNSData release.
stream := ReadStream on: rawData.
form := Form fromBinaryStream: stream.

%%
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki