82 lines
3.3 KiB
Mathematica
82 lines
3.3 KiB
Mathematica
|
|
// UIImage+AFNetworking.m
|
||
|
|
//
|
||
|
|
// Copyright (c) 2011 Gowalla (http://gowalla.com/)
|
||
|
|
//
|
||
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||
|
|
// of this software and associated documentation files (the "Software"), to deal
|
||
|
|
// in the Software without restriction, including without limitation the rights
|
||
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||
|
|
// copies of the Software, and to permit persons to whom the Software is
|
||
|
|
// furnished to do so, subject to the following conditions:
|
||
|
|
//
|
||
|
|
// The above copyright notice and this permission notice shall be included in
|
||
|
|
// all copies or substantial portions of the Software.
|
||
|
|
//
|
||
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||
|
|
// THE SOFTWARE.
|
||
|
|
|
||
|
|
#import "UIImage+AFNetworking.h"
|
||
|
|
|
||
|
|
@implementation UIImage (AFNetworking)
|
||
|
|
|
||
|
|
+ (UIImage *)imageByScalingAndCroppingImage:(UIImage *)image size:(CGSize)size {
|
||
|
|
if (image == nil) {
|
||
|
|
return nil;
|
||
|
|
} else if (CGSizeEqualToSize(image.size, size) || CGSizeEqualToSize(size, CGSizeZero)) {
|
||
|
|
return image;
|
||
|
|
}
|
||
|
|
|
||
|
|
CGSize scaledSize = size;
|
||
|
|
CGPoint thumbnailPoint = CGPointZero;
|
||
|
|
|
||
|
|
CGFloat widthFactor = size.width / image.size.width;
|
||
|
|
CGFloat heightFactor = size.height / image.size.height;
|
||
|
|
CGFloat scaleFactor = (widthFactor > heightFactor) ? widthFactor : heightFactor;
|
||
|
|
scaledSize.width = image.size.width * scaleFactor;
|
||
|
|
scaledSize.width = image.size.height * scaleFactor;
|
||
|
|
if (widthFactor > heightFactor) {
|
||
|
|
thumbnailPoint.y = (size.height - scaledSize.height) * 0.5;
|
||
|
|
} else if (widthFactor < heightFactor) {
|
||
|
|
thumbnailPoint.x = (size.width - scaledSize.width) * 0.5;
|
||
|
|
}
|
||
|
|
|
||
|
|
UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
|
||
|
|
[image drawInRect:CGRectMake(thumbnailPoint.x, thumbnailPoint.y, scaledSize.width, scaledSize.height)];
|
||
|
|
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
|
||
|
|
UIGraphicsEndImageContext();
|
||
|
|
|
||
|
|
return newImage;
|
||
|
|
}
|
||
|
|
|
||
|
|
+ (UIImage *)imageByRoundingCornersOfImage:(UIImage *)image corners:(UIRectCorner)corners cornerRadii:(CGSize)radii {
|
||
|
|
if (image == nil) {
|
||
|
|
return nil;
|
||
|
|
} else if(UIGraphicsBeginImageContextWithOptions != NULL) {
|
||
|
|
UIGraphicsBeginImageContextWithOptions(image.size, NO, 0.0);
|
||
|
|
} else {
|
||
|
|
UIGraphicsBeginImageContext(image.size);
|
||
|
|
}
|
||
|
|
|
||
|
|
CGContextRef context = UIGraphicsGetCurrentContext();
|
||
|
|
CGContextBeginPath(context);
|
||
|
|
CGContextAddPath(context, [[UIBezierPath bezierPathWithRoundedRect:CGRectMake(0.0f, 0.0f, image.size.width, image.size.height) byRoundingCorners:corners cornerRadii:radii] CGPath]);
|
||
|
|
CGContextClosePath(context);
|
||
|
|
CGContextClip(context);
|
||
|
|
|
||
|
|
CGRect rect = CGRectMake(0.0f, 0.0f, image.size.width, image.size.height);
|
||
|
|
[image drawInRect:rect];
|
||
|
|
|
||
|
|
UIImage *newImage = UIGraphicsGetImageFromCurrentImageContext();
|
||
|
|
UIGraphicsEndImageContext();
|
||
|
|
|
||
|
|
return newImage;
|
||
|
|
|
||
|
|
}
|
||
|
|
|
||
|
|
@end
|