ソースを参照

RA Image
添加旋转图片功能,用于修正拍摄的照片方向。
解决p拍摄裁图位置不对的bug。

Ray Zhang 9 年 前
コミット
030098027b

BIN
Ants Contract/AntsContract.xcworkspace/xcuserdata/Ray.xcuserdatad/UserInterfaceState.xcuserstate


+ 6 - 0
RA Image/RA Image.xcodeproj/project.pbxproj

@@ -53,6 +53,7 @@
 		71114AF11EB2DEF900D2200C /* ScannerControllerView.m in Sources */ = {isa = PBXBuildFile; fileRef = 71114AE91EB2DEF900D2200C /* ScannerControllerView.m */; };
 		71114AF21EB2DEF900D2200C /* ScannerViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 71114AEB1EB2DEF900D2200C /* ScannerViewController.m */; };
 		71114AF31EB2DEF900D2200C /* SCShapeView.m in Sources */ = {isa = PBXBuildFile; fileRef = 71114AED1EB2DEF900D2200C /* SCShapeView.m */; };
+		71749AFD1ECC199300018EEC /* UIImage+fixOrientation.m in Sources */ = {isa = PBXBuildFile; fileRef = 71749AFC1ECC199300018EEC /* UIImage+fixOrientation.m */; };
 		71961CB01EC00DDD002C4096 /* RANetworkTaskDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = 71961CAB1EC00DDD002C4096 /* RANetworkTaskDelegate.m */; };
 		71961CB11EC00DDD002C4096 /* RAUploadManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 71961CAD1EC00DDD002C4096 /* RAUploadManager.m */; };
 		71961CB21EC00DDD002C4096 /* RAUploadOperation.m in Sources */ = {isa = PBXBuildFile; fileRef = 71961CAF1EC00DDD002C4096 /* RAUploadOperation.m */; };
@@ -155,6 +156,8 @@
 		71114AEB1EB2DEF900D2200C /* ScannerViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ScannerViewController.m; path = ../../common/ScannerViewController.m; sourceTree = "<group>"; };
 		71114AEC1EB2DEF900D2200C /* SCShapeView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = SCShapeView.h; path = ../../common/SCShapeView.h; sourceTree = "<group>"; };
 		71114AED1EB2DEF900D2200C /* SCShapeView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = SCShapeView.m; path = ../../common/SCShapeView.m; sourceTree = "<group>"; };
+		71749AFB1ECC199300018EEC /* UIImage+fixOrientation.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "UIImage+fixOrientation.h"; path = "../../common/UIImage+fixOrientation.h"; sourceTree = "<group>"; };
+		71749AFC1ECC199300018EEC /* UIImage+fixOrientation.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = "UIImage+fixOrientation.m"; path = "../../common/UIImage+fixOrientation.m"; sourceTree = "<group>"; };
 		71961CAA1EC00DDD002C4096 /* RANetworkTaskDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RANetworkTaskDelegate.h; path = ../../common/upload/RANetworkTaskDelegate.h; sourceTree = "<group>"; };
 		71961CAB1EC00DDD002C4096 /* RANetworkTaskDelegate.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = RANetworkTaskDelegate.m; path = ../../common/upload/RANetworkTaskDelegate.m; sourceTree = "<group>"; };
 		71961CAC1EC00DDD002C4096 /* RAUploadManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = RAUploadManager.h; path = ../../common/upload/RAUploadManager.h; sourceTree = "<group>"; };
@@ -291,6 +294,8 @@
 		42BC2D061EBAC74A0016B30D /* Common */ = {
 			isa = PBXGroup;
 			children = (
+				71749AFB1ECC199300018EEC /* UIImage+fixOrientation.h */,
+				71749AFC1ECC199300018EEC /* UIImage+fixOrientation.m */,
 				42BC2D071EBAC75F0016B30D /* TouchImageView.h */,
 				42BC2D081EBAC75F0016B30D /* TouchImageView.m */,
 				71F8D1CC1EC955BF002003DB /* UIView+Toast.h */,
@@ -627,6 +632,7 @@
 				42A829C31EBC0F8300898B8A /* ManufacturerListController.m in Sources */,
 				71114AB61EB1C5C800D2200C /* NSString+Base64.m in Sources */,
 				71114AA61EB1C50D00D2200C /* RAUtils.m in Sources */,
+				71749AFD1ECC199300018EEC /* UIImage+fixOrientation.m in Sources */,
 				71DCDC3C1EAF234E00BA045A /* AppDelegate.m in Sources */,
 				71961CB21EC00DDD002C4096 /* RAUploadOperation.m in Sources */,
 				429F25221EC9AA4200C22ABB /* TakePhotoPreviewController.m in Sources */,

BIN
RA Image/RA Image.xcodeproj/project.xcworkspace/xcuserdata/ray.xcuserdatad/UserInterfaceState.xcuserstate


+ 59 - 1
RA Image/RA Image/TakePhotoPreviewController.m

@@ -8,6 +8,7 @@
 
 #import "TakePhotoPreviewController.h"
 #import "const.h"
+#import "UIImage+fixOrientation.h"
 
 @interface TakePhotoPreviewController ()<UIScrollViewDelegate>
 
@@ -33,7 +34,10 @@
     
     [self.previewScroll addSubview:self.imgView];
 }
-
+- (void)setPreImage:(UIImage *)preImage {
+    _preImage = [preImage fixOrientation];
+    //    self.nameLabel.text = name;
+}
 - (void)viewDidLayoutSubviews {
     [super viewDidLayoutSubviews];
     
@@ -112,6 +116,11 @@
 }
 
 - (IBAction)usePhotoClick:(UIButton *)sender {
+    
+    [self testimg];
+   // [self.navigationController popViewControllerAnimated:YES];
+    return;
+    
     if (self.photoHandler) {
         
         CGRect imgRect = self.imgView.frame;
@@ -145,7 +154,56 @@
     }
     [self.navigationController popViewControllerAnimated:YES];
 }
+-(void) testimg
+{
+    UIImageOrientation o =self.preImage.imageOrientation;
+    
+    CGRect imgviewRect = self.imgView.frame;
+    CGRect editRect = self.editContainer.frame;
+    CGPoint offset= self.previewScroll.contentOffset;
+    
+    double sscale=self.previewScroll.zoomScale;
+    CGSize contentsize=self.previewScroll.contentSize;
+    
+    CGRect imgScreenRect = CGRectMake(imgviewRect.origin.x-offset.x, imgviewRect.origin.y-offset.y, imgviewRect.size.width, imgviewRect.size.height);
+    
+    double scale = imgScreenRect.size.width/self.preImage.size.width;//1;//self.previewScroll.zoomScale*(imgviewRect.size.width/self.preImage.size.width);
+    CGRect rectimg = CGRectMake((editRect.origin.x-imgScreenRect.origin.x)/scale, (editRect.origin.y-imgScreenRect.origin.y)/scale, editRect.size.width/scale, editRect.size.height/scale);
+//    UIImage * newimg=[UIImage imageWithCGImage:self.preImage.CGImage scale:1 orientation:UIImageOrientationUp];
+    UIImage * img= [self captureImageWithImage:self.preImage inRect:rectimg];//
+    UIImage* img1 =[self getSubImage:rectimg img:self.preImage];
+    return;
+}
+
 
+-(UIImage*)getSubImage:(CGRect)rect img:(UIImage*)img
+{
+    
+    //将UIImage转换成CGImageRef
+    CGImageRef sourceImageRef = [img CGImage];
+    
+    //按照给定的矩形区域进行剪裁
+    CGImageRef newImageRef = CGImageCreateWithImageInRect(sourceImageRef, rect);
+    
+    //将CGImageRef转换成UIImage
+    UIImage *newImage = [UIImage imageWithCGImage:newImageRef];
+    
+    //返回剪裁后的图片
+    return newImage;
+    
+    
+    CGImageRef subImageRef = CGImageCreateWithImageInRect(img.CGImage, rect);
+    UIImage* debugimg = [UIImage imageWithCGImage:subImageRef];
+    CGRect smallBounds = CGRectMake(0, 0, CGImageGetWidth(subImageRef), CGImageGetHeight(subImageRef));
+    
+    UIGraphicsBeginImageContext(smallBounds.size);
+    CGContextRef context = UIGraphicsGetCurrentContext();
+    CGContextDrawImage(context, smallBounds, subImageRef);
+    UIImage* smallImage = [UIImage imageWithCGImage:subImageRef];
+    UIGraphicsEndImageContext();
+    
+    return smallImage;
+}
 #pragma mark - UIScrollViewDelegate
 
 - (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {

+ 13 - 0
common/UIImage+fixOrientation.h

@@ -0,0 +1,13 @@
+//
+//  UIImage+fixOrientation.h
+//  testCamera
+//
+//  Created by Ray on 17/05/2017.
+//  Copyright © 2017 R&J. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import <UIKit/UIKit.h>
+@interface UIImage (fixOrientation)
+- (UIImage *)fixOrientation;
+@end

+ 83 - 0
common/UIImage+fixOrientation.m

@@ -0,0 +1,83 @@
+//
+//  UIImage+fixOrientation.m
+//  testCamera
+//
+//  Created by Ray on 17/05/2017.
+//  Copyright © 2017 R&J. All rights reserved.
+//
+
+#import "UIImage+fixOrientation.h"
+
+@implementation UIImage (fixOrientation)
+- (UIImage *)fixOrientation {
+    
+    // No-op if the orientation is already correct
+    if (self.imageOrientation == UIImageOrientationUp) return self;
+    
+    // We need to calculate the proper transformation to make the image upright.
+    // We do it in 2 steps: Rotate if Left/Right/Down, and then flip if Mirrored.
+    CGAffineTransform transform = CGAffineTransformIdentity;
+    
+    switch (self.imageOrientation) {
+        case UIImageOrientationDown:
+        case UIImageOrientationDownMirrored:
+            transform = CGAffineTransformTranslate(transform, self.size.width, self.size.height);
+            transform = CGAffineTransformRotate(transform, M_PI);
+            break;
+            
+        case UIImageOrientationLeft:
+        case UIImageOrientationLeftMirrored:
+            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
+            transform = CGAffineTransformRotate(transform, M_PI_2);
+            break;
+            
+        case UIImageOrientationRight:
+        case UIImageOrientationRightMirrored:
+            transform = CGAffineTransformTranslate(transform, 0, self.size.height);
+            transform = CGAffineTransformRotate(transform, -M_PI_2);
+            break;
+    }
+    
+    switch (self.imageOrientation) {
+        case UIImageOrientationUpMirrored:
+        case UIImageOrientationDownMirrored:
+            transform = CGAffineTransformTranslate(transform, self.size.width, 0);
+            transform = CGAffineTransformScale(transform, -1, 1);
+            break;
+            
+        case UIImageOrientationLeftMirrored:
+        case UIImageOrientationRightMirrored:
+            transform = CGAffineTransformTranslate(transform, self.size.height, 0);
+            transform = CGAffineTransformScale(transform, -1, 1);
+            break;
+    }
+    
+    // Now we draw the underlying CGImage into a new context, applying the transform
+    // calculated above.
+    CGContextRef ctx = CGBitmapContextCreate(NULL, self.size.width, self.size.height,
+                                             CGImageGetBitsPerComponent(self.CGImage), 0,
+                                             CGImageGetColorSpace(self.CGImage),
+                                             CGImageGetBitmapInfo(self.CGImage));
+    CGContextConcatCTM(ctx, transform);
+    switch (self.imageOrientation) {
+        case UIImageOrientationLeft:
+        case UIImageOrientationLeftMirrored:
+        case UIImageOrientationRight:
+        case UIImageOrientationRightMirrored:
+            // Grr...
+            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.height,self.size.width), self.CGImage);
+            break;
+            
+        default:
+            CGContextDrawImage(ctx, CGRectMake(0,0,self.size.width,self.size.height), self.CGImage);
+            break;
+    }
+    
+    // And now we just create a new UIImage from the drawing context
+    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
+    UIImage *img = [UIImage imageWithCGImage:cgimg];
+    CGContextRelease(ctx);
+    CGImageRelease(cgimg);  
+    return img;  
+}
+@end