图像无法正确上传

时间:2016-04-05 06:48:53

标签: iphone express multer

我正在制作图片上传网站。

用户可以通过相机拍照并上传。

我正在使用iPhone 5S进行测试

但是,从iPhone上显示的照片在PC上以错误的方向显示,并且在移动设备中显示正确的方向。

在我的电脑中:

enter image description here

正确的方向(iPhone中的方向显示)

enter image description here

在我的上传过程中,我对图片一无所知,我使用Expressmulter

var storage = multer.diskStorage({
    destination: function(req, file, cb) {
        cb(null, 'public/uploads');
    },
    filename: function(req, file, cb) {
        var name = file.originalname;
        var dot = name.lastIndexOf('.');
        cb(null, name.slice(0, dot) + '-' + new Date().getTime() + name.slice(dot));
    }
});

var upload = multer({
    storage: storage,
    fileFilter: function(req, file, cb) {
        if (/\.(png|jpg)$/.test(file.originalname)) {
            cb(null, true);
        } else {
            cb(null, false);
        }
    }
});

var fileUpload = upload.single('file');
router.post('/ajaxupload', function(req, res) {
    fileUpload(req, res, function(err) {
        if (err) {
            res.json({
                success: false,
                message: 'upload failed'
            });
            return;
        }
        console.log(req.file);
        res.json({
            success: true,
            filename: req.file.path.replace(/public/, '')
        });
    });
});

1 个答案:

答案 0 :(得分:0)

调用图像方向更改的方法

- (UIImage *)changeOrientation:(UIImage *)image {

    if (image.imageOrientation == UIImageOrientationUp) return image;

    CGAffineTransform transform = CGAffineTransformIdentity;

    switch (image.imageOrientation) {
        case UIImageOrientationDown:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, image.size.height);
            transform = CGAffineTransformRotate(transform, M_PI);
            break;

        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformRotate(transform, M_PI_2);
            break;

        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, 0, image.size.height);
            transform = CGAffineTransformRotate(transform, -M_PI_2);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationUpMirrored:
            break;
    }

    switch (image.imageOrientation) {
        case UIImageOrientationUpMirrored:
        case UIImageOrientationDownMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.width, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;

        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRightMirrored:
            transform = CGAffineTransformTranslate(transform, image.size.height, 0);
            transform = CGAffineTransformScale(transform, -1, 1);
            break;
        case UIImageOrientationUp:
        case UIImageOrientationDown:
        case UIImageOrientationLeft:
        case UIImageOrientationRight:
            break;
    }

    CGContextRef ctx = CGBitmapContextCreate(NULL, image.size.width, image.size.height,
                                             CGImageGetBitsPerComponent(image.CGImage), 0,
                                                 CGImageGetColorSpace(image.CGImage),
                                             CGImageGetBitmapInfo(image.CGImage));
    CGContextConcatCTM(ctx, transform);
    switch (image.imageOrientation) {
        case UIImageOrientationLeft:
        case UIImageOrientationLeftMirrored:
        case UIImageOrientationRight:
        case UIImageOrientationRightMirrored:

            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.height,image.size.width), image.CGImage);
            break;

        default:
            CGContextDrawImage(ctx, CGRectMake(0,0,image.size.width,image.size.height), image.CGImage);
            break;
    }

    CGImageRef cgimg = CGBitmapContextCreateImage(ctx);
    UIImage *img = [UIImage imageWithCGImage:cgimg];
    CGContextRelease(ctx);
    CGImageRelease(cgimg);
    return img;
}