Skip to content

Commit

Permalink
fixed issue scaling upload image.
Browse files Browse the repository at this point in the history
  • Loading branch information
kishikawakatsumi committed Nov 14, 2010
1 parent 517445c commit f9aa0b3
Show file tree
Hide file tree
Showing 10 changed files with 271 additions and 41 deletions.
14 changes: 8 additions & 6 deletions Classes/FotolifeUploader.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#import "HatenaAtomPub.h"
#import "UserSettings.h"
#import "NSData+Base64.h"
#import "WBImage.h"
#import "UIImage+ProportionalFill.h"

@implementation FotolifeUploader
Expand Down Expand Up @@ -45,25 +46,26 @@ - (void)uploadImage:(UIImage *)image title:(NSString *)title {
UIImage *uploadImage = nil;

UserSettings *settings = [UserSettings sharedInstance];
CGSize imageSize = CGSizeMake(320.0f, 480.0f);
CGSize imageSize = image.size;
switch (settings.imageSize) {
case UserSettingsImageSizeSmall:
imageSize = CGSizeMake(160.0f, 240.0f);;
imageSize = CGSizeMake(160.0f, 240.0f);
break;
case UserSettingsImageSizeMedium:
imageSize = CGSizeMake(320.0f, 480.0f);;
imageSize = CGSizeMake(320.0f, 480.0f);
break;
case UserSettingsImageSizeLarge:
imageSize = CGSizeMake(480.0f, 720.0f);;
imageSize = CGSizeMake(480.0f, 720.0f);
break;
case UserSettingsImageSizeExtraLarge:
imageSize = CGSizeMake(640.0f, 960.0f);;
imageSize = CGSizeMake(640.0f, 960.0f);
break;
default:
break;
}

uploadImage = [image imageScaledToFitSize:imageSize];
// uploadImage = [image imageScaledToFitSize:imageSize];
uploadImage = [image rotateAndScaleFromCameraWithMaxSize:MAX(imageSize.width, imageSize.height)];

HatenaAtomPub *atomPub = [[HatenaAtomPub alloc] init];
NSMutableURLRequest *request = [atomPub makeRequestWithURI:@"http://f.hatena.ne.jp/atom/post" method:@"POST"];
Expand Down
10 changes: 8 additions & 2 deletions Classes/SettingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,15 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ImageSizeCell"] autorelease];
cell.selectionStyle = UITableViewCellSelectionStyleNone;

UISegmentedControl *imageSizeSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"S", @"M", @"L", @"XL", nil]];
UIView *transparentBackground = [[UIView alloc] initWithFrame:CGRectZero];
transparentBackground.backgroundColor = [UIColor clearColor];
cell.backgroundView = transparentBackground;
[transparentBackground release];

UISegmentedControl *imageSizeSegment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"S", @"M", @"L", @"XL", NSLocalizedString(@"Original", nil), nil]];
imageSizeSegment.segmentedControlStyle = UISegmentedControlStyleBar;
imageSizeSegment.autoresizingMask = UIViewAutoresizingFlexibleWidth;
imageSizeSegment.frame = CGRectMake(9.0f, 0.0f, 302.0f, 46.0f);
imageSizeSegment.frame = CGRectMake(9.0f, 7.0f, 302.0f, 30.0f);
imageSizeSegment.selectedSegmentIndex = settings.imageSize;
[imageSizeSegment addTarget:self action:@selector(imageSizeChanged:) forControlEvents:UIControlEventValueChanged];

Expand Down
60 changes: 36 additions & 24 deletions Classes/UIImage+ProportionalFill.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ @implementation UIImage (MGProportionalFill)


- (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resizeMethod
{
CGFloat scale = 1.0f;
if ([self respondsToSelector:@selector(scale)]) {
scale = self.scale;
}

float sourceWidth = [self size].width * scale;
float sourceHeight = [self size].height * scale;
{
float imageScaleFactor = 1.0;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([self respondsToSelector:@selector(scale)]) {
imageScaleFactor = [self scale];
}
#endif

float sourceWidth = [self size].width * imageScaleFactor;
float sourceHeight = [self size].height * imageScaleFactor;
float targetWidth = fitSize.width;
float targetHeight = fitSize.height;
BOOL cropping = !(resizeMethod == MGImageResizeScale);
Expand Down Expand Up @@ -86,22 +88,32 @@ - (UIImage *)imageToFitSize:(CGSize)fitSize method:(MGImageResizingMethod)resize
}

// Create appropriately modified image.
UIImage *image;
if (UIGraphicsBeginImageContextWithOptions) {
UIGraphicsBeginImageContextWithOptions(destRect.size, NO, 0.0); // 0.0 for scale means "correct scale for device's main screen".
} else {
UIGraphicsBeginImageContext(destRect.size);
}
CGImageRef sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect); // cropping happens here.
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) {
image = [UIImage imageWithCGImage:sourceImg scale:0.0 orientation:self.imageOrientation]; // create cropped UIImage.
} else {
image = [UIImage imageWithCGImage:sourceImg];
}
[image drawInRect:destRect]; // the actual scaling happens here, and orientation is taken care of automatically.
CGImageRelease(sourceImg);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
UIImage *image = nil;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 40000
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 4.0) {
UIGraphicsBeginImageContextWithOptions(destRect.size, NO, 0.0); // 0.0 for scale means "correct scale for device's main screen".
CGImageRef sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect); // cropping happens here.
image = [UIImage imageWithCGImage:sourceImg scale:0.0 orientation:self.imageOrientation]; // create cropped UIImage.
[image drawInRect:destRect]; // the actual scaling happens here, and orientation is taken care of automatically.
CGImageRelease(sourceImg);
image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
}
#endif
if (!image) {
// Try older method.
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGContextRef context = CGBitmapContextCreate(NULL, fitSize.width, fitSize.height, 8, (fitSize.width * 4),
colorSpace, kCGImageAlphaPremultipliedLast);
CGImageRef sourceImg = CGImageCreateWithImageInRect([self CGImage], sourceRect);
CGContextDrawImage(context, destRect, sourceImg);
CGImageRelease(sourceImg);
CGImageRef finalImage = CGBitmapContextCreateImage(context);
CGContextRelease(context);
CGColorSpaceRelease(colorSpace);
image = [UIImage imageWithCGImage:finalImage];
CGImageRelease(finalImage);
}

return image;
}
Expand Down
1 change: 1 addition & 0 deletions Classes/UserSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ typedef enum {
UserSettingsImageSizeMedium = 1,
UserSettingsImageSizeLarge = 2,
UserSettingsImageSizeExtraLarge = 3,
UserSettingsImageSizeOriginal = 4,
} UserSettingsImageSize;

@interface UserSettings : NSObject <NSCoding> {
Expand Down
24 changes: 24 additions & 0 deletions Classes/WBImage.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// WBImage.h -- extra UIImage methods
// by allen brunson march 29 2009

#ifndef WBIMAGE_H
#define WBIMAGE_H

#import <UIKit/UIKit.h>

@interface UIImage (WBImage)

// rotate UIImage to any angle
-(UIImage*)rotate:(UIImageOrientation)orient;

// rotate and scale image from iphone camera
-(UIImage*)rotateAndScaleFromCameraWithMaxSize:(CGFloat)maxSize;

// scale this image to a given maximum width and height
-(UIImage*)scaleWithMaxSize:(CGFloat)maxSize;
-(UIImage*)scaleWithMaxSize:(CGFloat)maxSize
quality:(CGInterpolationQuality)quality;

@end

#endif // WBIMAGE_H
179 changes: 179 additions & 0 deletions Classes/WBImage.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
// WBImage.mm -- extra UIImage methods
// by allen brunson march 29 2009

#include "WBImage.h"

static inline CGFloat degreesToRadians(CGFloat degrees)
{
return M_PI * (degrees / 180.0);
}

static inline CGSize swapWidthAndHeight(CGSize size)
{
CGFloat swap = size.width;

size.width = size.height;
size.height = swap;

return size;
}

@implementation UIImage (WBImage)

// rotate an image to any 90-degree orientation, with or without mirroring.
// original code by kevin lohman, heavily modified by yours truly.
// http://blog.logichigh.com/2008/06/05/uiimage-fix/

-(UIImage*)rotate:(UIImageOrientation)orient
{
CGRect bnds = CGRectZero;
UIImage* copy = nil;
CGContextRef ctxt = nil;
CGRect rect = CGRectZero;
CGAffineTransform tran = CGAffineTransformIdentity;

bnds.size = self.size;
rect.size = self.size;

switch (orient)
{
case UIImageOrientationUp:
return self;

case UIImageOrientationUpMirrored:
tran = CGAffineTransformMakeTranslation(rect.size.width, 0.0);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
break;

case UIImageOrientationDown:
tran = CGAffineTransformMakeTranslation(rect.size.width,
rect.size.height);
tran = CGAffineTransformRotate(tran, degreesToRadians(180.0));
break;

case UIImageOrientationDownMirrored:
tran = CGAffineTransformMakeTranslation(0.0, rect.size.height);
tran = CGAffineTransformScale(tran, 1.0, -1.0);
break;

case UIImageOrientationLeft:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(0.0, rect.size.width);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;

case UIImageOrientationLeftMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height,
rect.size.width);
tran = CGAffineTransformScale(tran, -1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(-90.0));
break;

case UIImageOrientationRight:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeTranslation(rect.size.height, 0.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;

case UIImageOrientationRightMirrored:
bnds.size = swapWidthAndHeight(bnds.size);
tran = CGAffineTransformMakeScale(-1.0, 1.0);
tran = CGAffineTransformRotate(tran, degreesToRadians(90.0));
break;

default:
// orientation value supplied is invalid
assert(false);
return nil;
}

UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();

switch (orient)
{
case UIImageOrientationLeft:
case UIImageOrientationLeftMirrored:
case UIImageOrientationRight:
case UIImageOrientationRightMirrored:
CGContextScaleCTM(ctxt, -1.0, 1.0);
CGContextTranslateCTM(ctxt, -rect.size.height, 0.0);
break;

default:
CGContextScaleCTM(ctxt, 1.0, -1.0);
CGContextTranslateCTM(ctxt, 0.0, -rect.size.height);
break;
}

CGContextConcatCTM(ctxt, tran);
CGContextDrawImage(ctxt, rect, self.CGImage);

copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return copy;
}

-(UIImage*)rotateAndScaleFromCameraWithMaxSize:(CGFloat)maxSize
{
UIImage* imag = self;

imag = [imag rotate:imag.imageOrientation];
imag = [imag scaleWithMaxSize:maxSize];

return imag;
}

-(UIImage*)scaleWithMaxSize:(CGFloat)maxSize
{
return [self scaleWithMaxSize:maxSize quality:kCGInterpolationHigh];
}

-(UIImage*)scaleWithMaxSize:(CGFloat)maxSize
quality:(CGInterpolationQuality)quality
{
CGRect bnds = CGRectZero;
UIImage* copy = nil;
CGContextRef ctxt = nil;
CGRect orig = CGRectZero;
CGFloat rtio = 0.0;
CGFloat scal = 1.0;

bnds.size = self.size;
orig.size = self.size;
rtio = orig.size.width / orig.size.height;

if ((orig.size.width <= maxSize) && (orig.size.height <= maxSize))
{
return self;
}

if (rtio > 1.0)
{
bnds.size.width = maxSize;
bnds.size.height = maxSize / rtio;
}
else
{
bnds.size.width = maxSize * rtio;
bnds.size.height = maxSize;
}

UIGraphicsBeginImageContext(bnds.size);
ctxt = UIGraphicsGetCurrentContext();

scal = bnds.size.width / orig.size.width;
CGContextSetInterpolationQuality(ctxt, quality);
CGContextScaleCTM(ctxt, scal, -scal);
CGContextTranslateCTM(ctxt, 0.0, -orig.size.height);
CGContextDrawImage(ctxt, orig, self.CGImage);

copy = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

return copy;
}

@end
2 changes: 1 addition & 1 deletion English.lproj/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ <h1>Hatena Bookmark<br />(Social Bookmark)</h1>
<li>Read, Edit, Delete your bookmarks.</li>
</ul>
<p>
<strong>Hatena touch ver. 1.2.4</strong><br />
<strong>Hatena touch ver. 1.2.5</strong><br />
<a href="http://kishikawakatsumi.com">KISHIKAWA Katsumi</a>
</p>
</body>
Expand Down
2 changes: 1 addition & 1 deletion HatenaTouch-Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>1.2.4</string>
<string>1.2.5</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>NSMainNibFile</key>
Expand Down
Loading

0 comments on commit f9aa0b3

Please sign in to comment.