Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
- {
- //Copy the image to the userImage variable
- [picker dismissModalViewControllerAnimated:YES];
- userImage = nil;
- //Rotate & resize image
- userImage = [self resizeAndRotatePhoto:(UIImage *)[info objectForKey:UIImagePickerControllerOriginalImage]];
- NSLog(@"Userimage size, width: %f , height: %f", userImage.size.width , userImage.size.height);
- //Update the image form field with the image and set the image in the controller
- NSLog(@"Button size is height: %f , width: %f" , userImageAvatarButton.frame.size.height , userImageAvatarButton.frame.size.width);
- [userImageAvatarButton.layer setMasksToBounds:YES];
- [userImageAvatarButton.layer setCornerRadius:3.0];
- [userImageAvatarButton setImage:userImage forState:UIControlStateNormal];
- }
- 2012-05-07 17:38:07.995 NewApp[10666:707] Userimage size, width: 1936.000000 , height: 1936.000000
- 2012-05-07 17:38:08.000 NewApp[10666:707] Button size is height: 60.000000 , width: 60.000000
- - (UIImage *)resizeAndRotatePhoto:(UIImage *)source
- {
- if( source.imageOrientation == UIImageOrientationRight )
- {
- source = [self rotateImage:source byDegrees:90];
- }
- if( userImage.imageOrientation == UIImageOrientationLeft)
- {
- source = [self rotateImage:source byDegrees:-90];
- }
- CGFloat x,y;
- CGFloat size;
- if( source.size.width > source.size.height ){
- size = source.size.height;
- x = (source.size.width - source.size.height)/2;
- y = 0;
- }
- else {
- size = source.size.width;
- x = 0;
- y = (source.size.height - source.size.width)/2;
- }
- CGImageRef imageRef = CGImageCreateWithImageInRect([source CGImage], CGRectMake(x,y,size,size) );
- return [UIImage imageWithCGImage:imageRef];
- }
- // UIImage+SimpleResize.m
- //
- // Created by Robert Ryan on 5/19/11.
- #import "UIImage+SimpleResize.h"
- @implementation UIImage (SimpleResize)
- - (UIImage*)imageByScalingToSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode
- {
- if (contentMode == UIViewContentModeScaleToFill)
- {
- return [self imageByScalingToFillSize:newSize];
- }
- else if ((contentMode == UIViewContentModeScaleAspectFill) ||
- (contentMode == UIViewContentModeScaleAspectFit))
- {
- CGFloat horizontalRatio = self.size.width / newSize.width;
- CGFloat verticalRatio = self.size.height / newSize.height;
- CGFloat ratio;
- if (contentMode == UIViewContentModeScaleAspectFill)
- ratio = MIN(horizontalRatio, verticalRatio);
- else
- ratio = MAX(horizontalRatio, verticalRatio);
- CGSize sizeForAspectScale = CGSizeMake(self.size.width / ratio, self.size.height / ratio);
- UIImage *image = [self imageByScalingToFillSize:sizeForAspectScale];
- // if we're doing aspect fill, then the image still needs to be cropped
- if (contentMode == UIViewContentModeScaleAspectFill)
- {
- CGRect subRect = CGRectMake(floor((sizeForAspectScale.width - newSize.width) / 2.0),
- floor((sizeForAspectScale.height - newSize.height) / 2.0),
- newSize.width,
- newSize.height);
- image = [image imageByCroppingToBounds:subRect];
- }
- return image;
- }
- return nil;
- }
- - (UIImage *)imageByCroppingToBounds:(CGRect)bounds
- {
- CGImageRef imageRef = CGImageCreateWithImageInRect([self CGImage], bounds);
- UIImage *croppedImage = [UIImage imageWithCGImage:imageRef];
- CGImageRelease(imageRef);
- return croppedImage;
- }
- - (UIImage*)imageByScalingToFillSize:(CGSize)newSize
- {
- UIGraphicsBeginImageContext(newSize);
- [self drawInRect:CGRectMake(0, 0, newSize.width, newSize.height)];
- UIImage* image = UIGraphicsGetImageFromCurrentImageContext();
- UIGraphicsEndImageContext();
- return image;
- }
- - (UIImage*)imageByScalingAspectFillSize:(CGSize)newSize
- {
- return [self imageByScalingToSize:newSize contentMode:UIViewContentModeScaleAspectFill];
- }
- - (UIImage*)imageByScalingAspectFitSize:(CGSize)newSize
- {
- return [self imageByScalingToSize:newSize contentMode:UIViewContentModeScaleAspectFit];
- }
- @end
- // UIImage+SimpleResize.h
- //
- // Created by Robert Ryan on 5/19/11.
- #import <Foundation/Foundation.h>
- /** Image resizing category.
- *
- * Modified by Robert Ryan on 5/19/11.
- *
- * Inspired by http://ofcodeandmen.poltras.com/2008/10/30/undocumented-uiimage-resizing/
- * but adjusted to support AspectFill and AspectFit modes.
- */
- @interface UIImage (SimpleResize)
- /** Resize the image to be the required size, stretching it as needed.
- *
- * @param newSize The new size of the image.
- * @param contentMode The `UIViewContentMode` to be applied when resizing image.
- * Either `UIViewContentModeScaleToFill`, `UIViewContentModeScaleAspectFill`, or
- * `UIViewContentModeScaleAspectFit`.
- *
- * @return Return `UIImage` of resized image.
- */
- - (UIImage*)imageByScalingToSize:(CGSize)newSize contentMode:(UIViewContentMode)contentMode;
- /** Crop the image to be the required size.
- *
- * @param bounds The bounds to which the new image should be cropped.
- *
- * @return Cropped `UIImage`.
- */
- - (UIImage *)imageByCroppingToBounds:(CGRect)bounds;
- /** Resize the image to be the required size, stretching it as needed.
- *
- * @param newSize The new size of the image.
- *
- * @return Resized `UIImage` of resized image.
- */
- - (UIImage*)imageByScalingToFillSize:(CGSize)newSize;
- /** Resize the image to fill the rectange of the specified size, preserving the aspect ratio, trimming if needed.
- *
- * @param newSize The new size of the image.
- *
- * @return Return `UIImage` of resized image.
- */
- - (UIImage*)imageByScalingAspectFillSize:(CGSize)newSize;
- /** Resize the image to fit within the required size, preserving the aspect ratio, with no trimming taking place.
- *
- * @param newSize The new size of the image.
- *
- * @return Return `UIImage` of resized image.
- */
- - (UIImage*)imageByScalingAspectFitSize:(CGSize)newSize;
- @end
- - (void)resizeImageAtPath:(NSString *)imagePath {
- // Create the image source (from path)
- CGImageSourceRef src = CGImageSourceCreateWithURL((__bridge CFURLRef) [NSURL fileURLWithPath:imagePath], NULL);
- // To create image source from UIImage, use this
- // NSData* pngData = UIImagePNGRepresentation(image);
- // CGImageSourceRef src = CGImageSourceCreateWithData((CFDataRef)pngData, NULL);
- // Create thumbnail options
- CFDictionaryRef options = (__bridge CFDictionaryRef) @{
- (id) kCGImageSourceCreateThumbnailWithTransform : @YES,
- (id) kCGImageSourceCreateThumbnailFromImageAlways : @YES,
- (id) kCGImageSourceThumbnailMaxPixelSize : @(640)
- };
- // Generate the thumbnail
- CGImageRef thumbnail = CGImageSourceCreateThumbnailAtIndex(src, 0, options);
- CFRelease(src);
- // Write the thumbnail at path
- CGImageWriteToFile(thumbnail, imagePath);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement