OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "ui/gfx/image/image.h" | 5 #include "ui/gfx/image/image.h" |
6 | 6 |
7 #import <UIKit/UIKit.h> | 7 #import <UIKit/UIKit.h> |
8 | 8 |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/memory/scoped_nsobject.h" | 10 #include "base/memory/scoped_nsobject.h" |
11 #include "ui/base/layout.h" | 11 #include "ui/base/layout.h" |
12 #include "ui/gfx/image/image_skia.h" | 12 #include "ui/gfx/image/image_skia.h" |
13 #include "ui/gfx/image/image_skia_util_ios.h" | 13 #include "ui/gfx/image/image_skia_util_ios.h" |
14 | 14 |
15 namespace gfx { | 15 namespace gfx { |
16 namespace internal { | 16 namespace internal { |
17 | 17 |
18 void PNGFromUIImage(UIImage* uiimage, std::vector<unsigned char>* png) { | 18 void PNGFromUIImage(UIImage* uiimage, std::vector<unsigned char>* png) { |
19 NSData* data = UIImagePNGRepresentation(uiimage); | 19 NSData* data = UIImagePNGRepresentation(uiimage); |
20 | 20 |
21 if ([data length] == 0) | 21 if ([data length] == 0) |
22 return; | 22 return; |
23 | 23 |
24 png->resize([data length]); | 24 png->resize([data length]); |
25 [data getBytes:&png->at(0) length:[data length]]; | 25 [data getBytes:&png->at(0) length:[data length]]; |
26 } | 26 } |
27 | 27 |
28 UIImage* CreateUIImageFromPNG(const std::vector<unsigned char>& png) { | 28 UIImage* CreateUIImageFromPNG(const std::vector<unsigned char>& png) { |
29 // iOS only supports one scale factor. | 29 ui::ScaleFactor scale_factor = ui::GetMaxScaleFactor(); |
30 std::vector<ui::ScaleFactor> supported_scale_factors = | |
31 ui::GetSupportedScaleFactors(); | |
32 DCHECK_EQ(1U, supported_scale_factors.size()); | |
33 if (supported_scale_factors.size() < 1) | |
34 return nil; | |
35 | |
36 ui::ScaleFactor scale_factor = supported_scale_factors[0]; | |
37 float scale = ui::GetScaleFactorScale(scale_factor); | 30 float scale = ui::GetScaleFactorScale(scale_factor); |
38 | 31 |
39 NSData* data = [NSData dataWithBytes:&png.front() length:png.size()]; | 32 NSData* data = [NSData dataWithBytes:&png.front() length:png.size()]; |
40 scoped_nsobject<UIImage> image ( | 33 scoped_nsobject<UIImage> image ( |
41 [[UIImage alloc] initWithData:data scale:scale]); | 34 [[UIImage alloc] initWithData:data scale:scale]); |
42 if (!image) { | 35 if (!image) { |
43 LOG(WARNING) << "Unable to decode PNG into UIImage."; | 36 LOG(WARNING) << "Unable to decode PNG into UIImage."; |
44 // Return a 16x16 red image to visually show error. | 37 // Return a 16x16 red image to visually show error. |
45 BOOL opaque = YES; | 38 BOOL opaque = YES; |
46 UIGraphicsBeginImageContextWithOptions(CGSizeMake(16, 16), opaque, scale); | 39 UIGraphicsBeginImageContextWithOptions(CGSizeMake(16, 16), opaque, scale); |
(...skipping 28 matching lines...) Expand all Loading... |
75 bitmap.allocPixels(); | 68 bitmap.allocPixels(); |
76 bitmap.eraseRGB(0xff, 0, 0); | 69 bitmap.eraseRGB(0xff, 0, 0); |
77 return new ImageSkia(bitmap); | 70 return new ImageSkia(bitmap); |
78 } | 71 } |
79 | 72 |
80 return new ImageSkia(ImageSkiaFromUIImage(uiimage)); | 73 return new ImageSkia(ImageSkiaFromUIImage(uiimage)); |
81 } | 74 } |
82 | 75 |
83 } // namespace internal | 76 } // namespace internal |
84 } // namespace gfx | 77 } // namespace gfx |
OLD | NEW |