Skip to content

Commit

Permalink
Increase the maximum allowable pasted/imported image size.
Browse files Browse the repository at this point in the history
  • Loading branch information
sprang committed Feb 28, 2014
1 parent eba0eee commit 06c414c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Classes/WDDrawingManager.m
Expand Up @@ -269,7 +269,7 @@ - (BOOL) createNewDrawingWithImage:(UIImage *)image imageName:(NSString *)imageN
return nil;
}

image = [image downsampleWithMaxDimension:1024];
image = [image downsampleWithMaxArea:4096*4096];

WDDrawing *drawing = [[WDDrawing alloc] initWithImage:image imageName:imageName];
return [self installDrawing:drawing withName:drawingName closeAfterSaving:YES] ? YES : NO;
Expand Down
1 change: 1 addition & 0 deletions Inkpad-Core/Additions/UIImage+Additions.h
Expand Up @@ -16,6 +16,7 @@
- (void) drawToFillRect:(CGRect)bounds;
- (UIImage *) rotatedImage:(int)rotation;
- (UIImage *) downsampleWithMaxDimension:(float)constraint;
- (UIImage *) downsampleWithMaxArea:(float)constraint;
- (UIImage *) JPEGify:(float)compressionFactor;

@end
18 changes: 18 additions & 0 deletions Inkpad-Core/Additions/UIImage+Additions.m
Expand Up @@ -86,6 +86,24 @@ - (UIImage *) downsampleWithMaxDimension:(float)constraint
return [self resizedImage:newSize interpolationQuality:kCGInterpolationHigh];
}

- (UIImage *) downsampleWithMaxArea:(float)maxArea
{
CGSize size = self.size;
double area = size.width * size.height;

if (area > maxArea) {
double scale = sqrt(maxArea) / sqrt(area);
size = WDMultiplySizeScalar(size, scale);
// whole pixel size
size = WDRoundSize(size);
} else if (self.imageOrientation == UIImageOrientationUp) {
// we're small enough and have the right orientation
return self;
}

return [self resizedImage:size interpolationQuality:kCGInterpolationHigh];
}

- (UIImage *) JPEGify:(float)compressionFactor
{
NSData * jpegData = UIImageJPEGRepresentation(self, compressionFactor);
Expand Down
14 changes: 9 additions & 5 deletions Inkpad-Core/Controllers/WDDrawingController.m
Expand Up @@ -1227,15 +1227,19 @@ - (void) excludePaths:(id)sender

- (void) placeImage:(UIImage *)image
{
image = [image downsampleWithMaxDimension:1024];
image = [image downsampleWithMaxArea:4096*4096];
WDImage *placedImage = [WDImage imageWithUIImage:image inDrawing:drawing_];

float scale = (drawing_.dimensions.width / 2) / image.size.width;
CGSize imageSize = image.size;
CGSize drawingSize = drawing_.dimensions;

double imageRatio = imageSize.width / imageSize.height;
double drawingRatio = drawingSize.width / drawingSize.height;
double scale = (drawingRatio > imageRatio) ? (drawingSize.height / imageSize.height) : (drawingSize.width / imageSize.width);
scale = (scale > 1) ? 1 : scale;

float width = scale * image.size.width;
float height = scale * image.size.height;
CGPoint ul = CGPointMake((drawing_.dimensions.width - width) / 2, (drawing_.dimensions.height - height) / 2);
imageSize = WDMultiplySizeScalar(imageSize, scale);
CGPoint ul = CGPointMake((drawingSize.width - imageSize.width) / 2, (drawingSize.height - imageSize.height) / 2);

CGAffineTransform transform = CGAffineTransformMakeTranslation(ul.x, ul.y);
transform = CGAffineTransformScale(transform, scale, scale);
Expand Down

0 comments on commit 06c414c

Please sign in to comment.