Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TIMOB-12189] set appropriate Blob mime-type when manipulating on images #3678

Merged
merged 3 commits into from
Jan 8, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
36 changes: 22 additions & 14 deletions iphone/Classes/TiBlob.m
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
//NOTE:FilesystemFile is conditionally compiled based on the filesystem module.
#import "TiFilesystemFileProxy.h"

static NSString *const MIMETYPE_PNG = @"image/png";
static NSString *const MIMETYPE_JPEG = @"image/jpeg";

@implementation TiBlob

-(void)dealloc
Expand Down Expand Up @@ -106,7 +109,7 @@ -(id)initWithImage:(UIImage*)image_
{
image = [image_ retain];
type = TiBlobTypeImage;
mimetype = [@"image/jpeg" retain];
mimetype = [([UIImageAlpha hasAlpha:image_] ? MIMETYPE_PNG : MIMETYPE_JPEG) copy];
}
return self;
}
Expand All @@ -117,7 +120,7 @@ -(id)initWithData:(NSData*)data_ mimetype:(NSString*)mimetype_
{
data = [data_ retain];
type = TiBlobTypeData;
mimetype = [mimetype_ retain];
mimetype = [mimetype_ copy];
}
return self;
}
Expand All @@ -128,7 +131,7 @@ -(id)initWithFile:(NSString*)path_
{
type = TiBlobTypeFile;
path = [path_ retain];
mimetype = [[Mimetypes mimeTypeForExtension:path] retain];
mimetype = [[Mimetypes mimeTypeForExtension:path] copy];
}
return self;
}
Expand Down Expand Up @@ -175,7 +178,7 @@ -(NSData*)data
}
case TiBlobTypeImage:
{
if ([@"image/png" isEqualToString:mimetype]) {
if ([mimetype isEqualToString:MIMETYPE_PNG]) {
return UIImagePNGRepresentation(image);
}
return UIImageJPEGRepresentation(image,1.0);
Expand Down Expand Up @@ -217,7 +220,7 @@ -(void)setImage:(UIImage *)image_
{
RELEASE_TO_NIL(image);
image = [image_ retain];
[self setMimeType:@"image/jpeg" type:TiBlobTypeImage];
[self setMimeType:([UIImageAlpha hasAlpha:image_] ? MIMETYPE_PNG : MIMETYPE_JPEG) type:TiBlobTypeImage];
}

-(NSString*)path
Expand Down Expand Up @@ -263,7 +266,7 @@ -(NSNumber*)length
-(void)setMimeType:(NSString*)mime type:(TiBlobType)type_
{
RELEASE_TO_NIL(mimetype);
mimetype = [mime retain];
mimetype = [mime copy];
type = type_;
}

Expand Down Expand Up @@ -302,7 +305,8 @@ - (id)imageWithAlpha:(id)args
[self ensureImageLoaded];
if (image!=nil)
{
return [[[TiBlob alloc] initWithImage:[UIImageAlpha imageWithAlpha:image]] autorelease];
TiBlob *blob = [[TiBlob alloc] initWithImage:[UIImageAlpha imageWithAlpha:image]];
return [blob autorelease];
}
return nil;
}
Expand All @@ -314,7 +318,8 @@ - (id)imageWithTransparentBorder:(id)args
{
ENSURE_SINGLE_ARG(args,NSObject);
NSUInteger size = [TiUtils intValue:args];
return [[[TiBlob alloc] initWithImage:[UIImageAlpha transparentBorderImage:size image:image]] autorelease];
TiBlob *blob = [[TiBlob alloc] initWithImage:[UIImageAlpha transparentBorderImage:size image:image]];
return [blob autorelease];
}
return nil;
}
Expand All @@ -326,7 +331,8 @@ - (id)imageWithRoundedCorner:(id)args
{
NSUInteger cornerSize = [TiUtils intValue:[args objectAtIndex:0]];
NSUInteger borderSize = [args count] > 1 ? [TiUtils intValue:[args objectAtIndex:1]] : 1;
return [[[TiBlob alloc] initWithImage:[UIImageRoundedCorner roundedCornerImage:cornerSize borderSize:borderSize image:image]] autorelease];
TiBlob *blob = [[TiBlob alloc] initWithImage:[UIImageRoundedCorner roundedCornerImage:cornerSize borderSize:borderSize image:image]];
return [blob autorelease];
}
return nil;
}
Expand All @@ -339,12 +345,12 @@ - (id)imageAsThumbnail:(id)args
NSUInteger size = [TiUtils intValue:[args objectAtIndex:0]];
NSUInteger borderSize = [args count] > 1 ? [TiUtils intValue:[args objectAtIndex:1]] : 1;
NSUInteger cornerRadius = [args count] > 2 ? [TiUtils intValue:[args objectAtIndex:2]] : 0;
return [[[TiBlob alloc] initWithImage:[UIImageResize thumbnailImage:size
TiBlob *blob = [[TiBlob alloc] initWithImage:[UIImageResize thumbnailImage:size
transparentBorder:borderSize
cornerRadius:cornerRadius
interpolationQuality:kCGInterpolationHigh
image:image]]
autorelease];
image:image]];
return [blob autorelease];
}
return nil;
}
Expand All @@ -357,7 +363,8 @@ - (id)imageAsResized:(id)args
ENSURE_ARG_COUNT(args,2);
NSUInteger width = [TiUtils intValue:[args objectAtIndex:0]];
NSUInteger height = [TiUtils intValue:[args objectAtIndex:1]];
return [[[TiBlob alloc] initWithImage:[UIImageResize resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationHigh image:image hires:NO]] autorelease];
TiBlob *blob = [[TiBlob alloc] initWithImage:[UIImageResize resizedImage:CGSizeMake(width, height) interpolationQuality:kCGInterpolationHigh image:image hires:NO]];
return [blob autorelease];
}
return nil;
}
Expand All @@ -374,7 +381,8 @@ - (id)imageAsCropped:(id)args
bounds.size.height = [TiUtils floatValue:@"height" properties:args def:imageSize.height];
bounds.origin.x = [TiUtils floatValue:@"x" properties:args def:(imageSize.width - bounds.size.width) / 2.0];
bounds.origin.y = [TiUtils floatValue:@"y" properties:args def:(imageSize.height - bounds.size.height) / 2.0];
return [[[TiBlob alloc] initWithImage:[UIImageResize croppedImage:bounds image:image]] autorelease];
TiBlob *blob = [[TiBlob alloc] initWithImage:[UIImageResize croppedImage:bounds image:image]];
return [blob autorelease];
}
return nil;
}
Expand Down