adium 2415:bbed84b1bd5c: Add an image method for returning an im...
commits at adium.im
commits at adium.im
Sat May 30 22:05:39 UTC 2009
details: http://hg.adium.im/adium/rev/bbed84b1bd5c
revision: 2415:bbed84b1bd5c
author: Zachary West <zacw at adium.im>
date: Sat May 30 18:05:31 2009 -0400
Add an image method for returning an image representation with a maximum size.
Use this new method for only uploading to pic.im images less-than the maximum allowed size.
diffs (145 lines):
diff -r 7f2d56a01424 -r bbed84b1bd5c Frameworks/AIUtilities Framework/Source/AIImageAdditions.h
--- a/Frameworks/AIUtilities Framework/Source/AIImageAdditions.h Sat May 30 17:08:43 2009 -0400
+++ b/Frameworks/AIUtilities Framework/Source/AIImageAdditions.h Sat May 30 18:05:31 2009 -0400
@@ -52,6 +52,8 @@
- (NSData *)GIFRepresentation;
- (NSData *)BMPRepresentation;
- (NSBitmapImageRep *)largestBitmapImageRep;
+- (NSData *)representationWithFileType:(NSBitmapImageFileType)fileType
+ maximumFileSize:(NSUInteger)maximumSize;
@end
diff -r 7f2d56a01424 -r bbed84b1bd5c Frameworks/AIUtilities Framework/Source/AIImageAdditions.m
--- a/Frameworks/AIUtilities Framework/Source/AIImageAdditions.m Sat May 30 17:08:43 2009 -0400
+++ b/Frameworks/AIUtilities Framework/Source/AIImageAdditions.m Sat May 30 18:05:31 2009 -0400
@@ -279,4 +279,67 @@
return extension;
}
+
+/*!
+ * @brief Retrieve an image rep with a maximum size
+ *
+ * Returns the NSData of an image representation.
+ *
+ * @param fileType The NSBitmapImageFileType to be outputted for sizing
+ * @param maximumSize The maximum size in bytes for the image
+ *
+ * @return the NSData representation using fileType
+ */
+- (NSData *)representationWithFileType:(NSBitmapImageFileType)fileType
+ maximumFileSize:(NSUInteger)maximumSize
+{
+ NSBitmapImageRep *imageRep = [self largestBitmapImageRep];
+
+ // If no rep is found, return nil.
+ if (!imageRep)
+ return nil;
+
+ NSData *data = [imageRep representationUsingType:fileType properties:nil];
+
+ // If no maximum size, return the base representation.
+ if (!maximumSize)
+ return data;
+
+ // Ratio of height/width
+ CGFloat ratio = (CGFloat)imageRep.pixelsHigh / (CGFloat)imageRep.pixelsWide;
+
+ // Loop until we're small enough to fit into our max size
+ while (data.length > maximumSize) {
+ // New width/height using our ratio
+ NSUInteger width = (imageRep.pixelsWide - 100);
+ NSUInteger height = ((CGFloat)imageRep.pixelsWide - 100.0)*ratio;
+
+ // Create a new rep with the lowered size
+ NSBitmapImageRep *newImageRep = [[[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
+ pixelsWide:width
+ pixelsHigh:height
+ bitsPerSample:imageRep.bitsPerSample
+ samplesPerPixel:imageRep.samplesPerPixel
+ hasAlpha:imageRep.hasAlpha
+ isPlanar:imageRep.isPlanar
+ colorSpaceName:NSCalibratedRGBColorSpace
+ bytesPerRow:imageRep.bytesPerRow
+ bitsPerPixel:imageRep.bitsPerPixel] autorelease];
+
+ // Draw the old rep into the new rep
+ [NSGraphicsContext saveGraphicsState];
+ [NSGraphicsContext setCurrentContext:[NSGraphicsContext graphicsContextWithBitmapImageRep:newImageRep]];
+ [imageRep drawInRect:NSMakeRect(0, 0, width, height)];
+ [NSGraphicsContext restoreGraphicsState];
+
+ // Override the old rep
+ imageRep = newImageRep;
+
+ // Grab a new representation
+ data = [imageRep representationUsingType:fileType properties:nil];
+ }
+
+ return nil;
+}
+
@end
diff -r 7f2d56a01424 -r bbed84b1bd5c Plugins/Image Uploading Plugin/AIImageUploaderPlugin.m
--- a/Plugins/Image Uploading Plugin/AIImageUploaderPlugin.m Sat May 30 17:08:43 2009 -0400
+++ b/Plugins/Image Uploading Plugin/AIImageUploaderPlugin.m Sat May 30 18:05:31 2009 -0400
@@ -18,6 +18,7 @@
#import <AIUtilities/AIWindowAdditions.h>
#import <AIUtilities/AIMenuAdditions.h>
+#import <AIUtilities/AIImageAdditions.h>
@interface AIImageUploaderPlugin()
- (NSImage *)currentImage;
diff -r 7f2d56a01424 -r bbed84b1bd5c Plugins/Image Uploading Plugin/AIPicImImageUploader.m
--- a/Plugins/Image Uploading Plugin/AIPicImImageUploader.m Sat May 30 17:08:43 2009 -0400
+++ b/Plugins/Image Uploading Plugin/AIPicImImageUploader.m Sat May 30 18:05:31 2009 -0400
@@ -12,9 +12,11 @@
#import <Adium/AIInterfaceControllerProtocol.h>
#import <AIUtilities/AIStringAdditions.h>
#import <AIUtilities/AIProgressDataUploader.h>
+#import <AIUtilities/AIImageAdditions.h>
#define MULTIPART_FORM_BOUNDARY @"bf5faadd239c17e35f91e6dafe1d2f96"
#define PIC_IM_URL @"http://api.tr.im/api/picim_url.xml?api_key=zghQN6sv5y0FkLPNlQAopm7qDQz6ItO33ENU21OBsy3dL1Kl"
+#define PIC_IM_MAX_SIZE 2500000
@interface AIPicImImageUploader()
- (id)initWithImage:(NSImage *)inImage
@@ -85,29 +87,21 @@
- (void)uploadImage
{
- NSBitmapImageRep *bitmapImageRep = nil;
+ NSMutableData *body = [NSMutableData data];
- for (NSImageRep *rep in image.representations) {
- if ([rep isKindOfClass:[NSBitmapImageRep class]]) {
- bitmapImageRep = (NSBitmapImageRep *)rep;
- break;
- }
- }
+ NSData *imageRepresentation = [image representationWithFileType:NSPNGFileType maximumFileSize:PIC_IM_MAX_SIZE];
- // This probably won't happen.
- if (!bitmapImageRep) {
- [uploader errorWithMessage:AILocalizedString(@"Unknown image type", nil) forChat:chat];
+ if (!imageRepresentation) {
+ [uploader errorWithMessage:AILocalizedString(@"Unable to upload", nil) forChat:chat];
return;
}
- NSMutableData *body = [NSMutableData data];
-
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", MULTIPART_FORM_BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"media\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
- [body appendData:[bitmapImageRep representationUsingType:NSPNGFileType properties:nil]];
+ [body appendData:imageRepresentation];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", MULTIPART_FORM_BOUNDARY] dataUsingEncoding:NSUTF8StringEncoding]];
-
+
NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys:
[NSString stringWithFormat:@"multipart/form-data; boundary=%@", MULTIPART_FORM_BOUNDARY], @"Content-type", nil];
More information about the commits
mailing list