Class: UIImage
- Inherits:
-
Object
- Object
- UIImage
- Defined in:
- lib/purplish-red/non-ui/ui_image.rb
Overview
Stuff I copied from MOCommon, some new
Constant Summary collapse
- M_PI =
3.14159265358979323846264338327950288
Class Method Summary collapse
Instance Method Summary collapse
- #height ⇒ Object
- #image_by_cropping_to_center_size(aSize) ⇒ Object
-
#image_by_cropping_to_rect(aRect) ⇒ Object
aRect is assumed to be in the “same scale” as self.
- #rotate_to_correct_orientation ⇒ Object
- #rotate_with_orientation(anOrientation) ⇒ Object
-
#scale_aspect_to_fill_size(aSize) ⇒ Object
Preserve aspect ratio while scaling to fill.
-
#scale_aspect_to_maximum_size(aSize) ⇒ Object
Preserve aspect ratio while scaling.
-
#scale_aspect_to_minimum_size(aSize) ⇒ Object
Preserve aspect ratio while scaling.
- #scale_aspect_to_size(size) ⇒ Object
- #scale_to_size(size) ⇒ Object
- #show_globally ⇒ Object
- #to_btn ⇒ Object
- #to_imgv ⇒ Object
- #width ⇒ Object
Class Method Details
.size_at_path(s) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 5 def self.size_at_path(s) imageFileURL = NSURL.fileURLWithPath(s) imageSource = CGImageSourceCreateWithURL(imageFileURL, nil) if imageSource.nil? #p "Error loading image" else imageProperties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, {KCGImageSourceShouldCache => false}) if imageProperties.nil? #p "Failed to get properties for image" else return CGSizeMake(imageProperties[KCGImagePropertyPixelWidth], imageProperties[KCGImagePropertyPixelHeight]) end end return CGSizeZero end |
Instance Method Details
#height ⇒ Object
40 41 42 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 40 def height size.height end |
#image_by_cropping_to_center_size(aSize) ⇒ Object
108 109 110 111 112 113 114 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 108 def image_by_cropping_to_center_size(aSize) if aSize.width/aSize.height > size.width/size.height return image_by_cropping_to_rect(CGRect.new([0, (size.height-aSize.height)/2], [aSize.width, aSize.height])) else return image_by_cropping_to_rect(CGRect.new([(size.width-aSize.width)/2, 0], [aSize.width, aSize.height])) end end |
#image_by_cropping_to_rect(aRect) ⇒ Object
aRect is assumed to be in the “same scale” as self
98 99 100 101 102 103 104 105 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 98 def image_by_cropping_to_rect(aRect) if aRect.is_a? CGRect cropped = CGImageCreateWithImageInRect(self.CGImage, [[aRect.origin.x*self.scale, aRect.origin.y*self.scale], [aRect.size.width*self.scale, aRect.size.height*self.scale]]) else cropped = CGImageCreateWithImageInRect(self.CGImage, [[aRect[0][0]*self.scale, aRect[0][1]*self.scale], [aRect[1][0]*self.scale, aRect[1][1]*self.scale]]) end UIImage.imageWithCGImage(cropped, scale:self.scale, orientation:self.imageOrientation) end |
#rotate_to_correct_orientation ⇒ Object
222 223 224 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 222 def rotate_to_correct_orientation rotate_with_orientation(imageOrientation) end |
#rotate_with_orientation(anOrientation) ⇒ Object
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 117 def rotate_with_orientation(anOrientation) #Front camera? We try to make fix it if (size.width == 640 && size.height == 480) || (size.width == 480 && size.height == 640) p 'Front camera photo' if anOrientation == UIImageOrientationUp #EXIF = 1 elsif anOrientation == UIImageOrientationDown #EXIF = 3 anOrientation = UIImageOrientationLeftMirrored elsif anOrientation == UIImageOrientationLeft #EXIF = 6 elsif anOrientation == UIImageOrientationRight #EXIF = 8 anOrientation = UIImageOrientationLeftMirrored elsif anOrientation == UIImageOrientationUpMirrored #EXIF = 2 elsif anOrientation == UIImageOrientationDownMirrored #EXIF = 4 elsif anOrientation == UIImageOrientationLeftMirrored #EXIF = 5 elsif UIImageOrientationRightMirrored #EXIF = 7 anOrientation = UIImageOrientationRightMirrored end elsif p 'Not front camera photo' end imgRef = self.CGImage width = CGImageGetWidth(imgRef) height = CGImageGetHeight(imgRef) transform = CGAffineTransformIdentity bounds = CGRect.new([0, 0], [width, height]) imageSize = CGSize.new(width, height) if anOrientation == UIImageOrientationUp #EXIF = 1 transform = CGAffineTransformIdentity elsif anOrientation == UIImageOrientationUpMirrored #EXIF = 2 transform = CGAffineTransformMakeTranslation(imageSize.width, 0.0) transform = CGAffineTransformScale(transform, -1.0, 1.0) elsif anOrientation == UIImageOrientationDown #EXIF = 3 transform = CGAffineTransformMakeTranslation(imageSize.width, imageSize.height) transform = CGAffineTransformRotate(transform, M_PI) elsif anOrientation == UIImageOrientationDownMirrored #EXIF = 4 transform = CGAffineTransformMakeTranslation(0.0, imageSize.height) transform = CGAffineTransformScale(transform, 1.0, -1.0) elsif anOrientation == UIImageOrientationLeftMirrored #EXIF = 5 boundHeight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = boundHeight transform = CGAffineTransformMakeTranslation(imageSize.height, imageSize.width) transform = CGAffineTransformScale(transform, -1.0, 1.0) transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0) elsif anOrientation == UIImageOrientationLeft #EXIF = 6 boundHeight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = boundHeight transform = CGAffineTransformMakeTranslation(0.0, imageSize.width) transform = CGAffineTransformRotate(transform, 3.0 * M_PI / 2.0) elsif anOrientation == UIImageOrientationRightMirrored #EXIF = 7 boundHeight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = boundHeight transform = CGAffineTransformMakeScale(-1.0, 1.0) transform = CGAffineTransformRotate(transform, M_PI / 2.0) elsif anOrientation == UIImageOrientationRight #EXIF = 8 boundHeight = bounds.size.height bounds.size.height = bounds.size.width bounds.size.width = boundHeight transform = CGAffineTransformMakeTranslation(imageSize.height, 0.0) transform = CGAffineTransformRotate(transform, M_PI / 2.0) end UIGraphicsBeginImageContext(bounds.size) context = UIGraphicsGetCurrentContext() if anOrientation == UIImageOrientationRight || anOrientation == UIImageOrientationLeft CGContextScaleCTM(context, -1, 1) CGContextTranslateCTM(context, -height, 0) elsif anOrientation == UIImageOrientationLeft || anOrientation == UIImageOrientationLeftMirrored || anOrientation == UIImageOrientationRightMirrored CGContextScaleCTM(context, 1, -1) CGContextTranslateCTM(context, 0, -width) else CGContextScaleCTM(context, 1, -1) CGContextTranslateCTM(context, 0, -height) end CGContextConcatCTM(context, transform) CGContextDrawImage(UIGraphicsGetCurrentContext(), CGRect.new([0, 0], [width, height]), imgRef) imageCopy = UIGraphicsGetImageFromCurrentImageContext() UIGraphicsEndImageContext() imageCopy end |
#scale_aspect_to_fill_size(aSize) ⇒ Object
Preserve aspect ratio while scaling to fill. Part of the content may be clipped
86 87 88 89 90 91 92 93 94 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 86 def scale_aspect_to_fill_size(aSize) if aSize.width/aSize.height > size.width/size.height croppedImg = image_by_cropping_to_center_size(CGSize.new(size.width, (size.width/aSize.width * aSize.height).to_i)) else croppedImg = image_by_cropping_to_center_size(CGSize.new((size.height/aSize.height * aSize.width).to_i, size.height)) end croppedImg.scale_to_size(aSize) end |
#scale_aspect_to_maximum_size(aSize) ⇒ Object
Preserve aspect ratio while scaling. E.g if aSize = (612,612), the longer side will be 612 and the shorter side will be at most 612
62 63 64 65 66 67 68 69 70 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 62 def scale_aspect_to_maximum_size(aSize) if aSize.width/aSize.height > self.size.width/self.size.height s = CGSize.new((aSize.height/size.height * size.width).to_i, aSize.height) else s = CGSize.new(aSize.width, (aSize.width/size.width * size.height).to_i) end scale_to_size(s) end |
#scale_aspect_to_minimum_size(aSize) ⇒ Object
Preserve aspect ratio while scaling. E.g if aSize = (612,612), the shorter side will be 612 and the shorter side will be at least 612
74 75 76 77 78 79 80 81 82 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 74 def scale_aspect_to_minimum_size(aSize) if aSize.width/aSize.height > self.size.width/self.size.height s = CGSize.new(aSize.width, (aSize.width/size.width * size.height).to_i) else s = CGSize.new((aSize.height/size.height * size.width).to_i, aSize.height) end scale_to_size(s) end |
#scale_aspect_to_size(size) ⇒ Object
45 46 47 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 45 def scale_aspect_to_size(size) scale_aspect_to_maximum_size(size) end |
#scale_to_size(size) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 50 def scale_to_size(size) colorSpace = CGColorSpaceCreateDeviceRGB() context = CGBitmapContextCreate(nil, size.width*scale, size.height*scale, 8, size.width*4*scale, colorSpace, KCGImageAlphaPremultipliedLast) imageReference = self.CGImage CGContextDrawImage(context, CGRectMake(0, 0, size.width*scale, size.height*scale), imageReference) copy = CGBitmapContextCreateImage(context) UIImage.imageWithCGImage(copy, scale:scale, orientation:imageOrientation) end |
#show_globally ⇒ Object
227 228 229 230 231 232 233 234 235 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 227 def show_globally btn = self.to_btn btn.addTarget(btn, action:'removeFromSuperview', forControlEvents:UIControlEventTouchUpInside) parent = App.delegate.window btn.backgroundColor = UIColor.redColor btn.center_x = parent.width/2 btn.center_y = parent.height/2 parent.addSubview(btn) end |
#to_btn ⇒ Object
23 24 25 26 27 28 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 23 def to_btn b = UIButton.(UIButtonTypeCustom) b.setImage(self, forState:UIControlStateNormal) b.frame = [[0, 0], [size.width, size.height]] b end |
#to_imgv ⇒ Object
30 31 32 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 30 def to_imgv UIImageView.alloc.initWithImage(self) end |
#width ⇒ Object
35 36 37 |
# File 'lib/purplish-red/non-ui/ui_image.rb', line 35 def width size.width end |