0

当我在肖像模式下使用手机拍照时,使用 UIImagePickerController,返回的 UIImage 项具有图像的顶部朝左和 imageOrientation 属性UIImageOrientationRight , // 90 deg CCW,这是预期的(我认为)。

当我通过 UIActivityViewController 共享图像时,所有活动似乎都正常运行,除了 Copy 活动。当我将图像粘贴到另一个应用程序(例如,消息)时,图像似乎忽略了 imageOrientation 属性。

有没有其他人看到这个/找到了解决方法?

已更新完整代码:(项目包含情节提要,其中一个按钮映射到self.takeImageButton- (IBAction)takeImageAndShare:(id)sender;

#import "ViewController.h"

@interface ViewController () <UIImagePickerControllerDelegate,UINavigationControllerDelegate>

@property (weak, nonatomic) IBOutlet UIButton *takeImageButton;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)takeImageAndShare:(id)sender {
    UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];
    imagePicker.delegate = self;
    imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePicker.popoverPresentationController.sourceView = self.takeImageButton;
    [self presentViewController:imagePicker animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    UIImage *chosenImage = info[UIImagePickerControllerOriginalImage];
    [picker dismissViewControllerAnimated:YES completion:NULL];

    NSLog(@"Image Orientation: %li",chosenImage.imageOrientation);

    // Now share the selected image
    UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[chosenImage] applicationActivities:nil];
    activityViewController.modalPresentationStyle = UIModalPresentationPopover;
    activityViewController.popoverPresentationController.sourceView = self.takeImageButton;
    [self presentViewController:activityViewController animated:YES completion:nil];
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker;
{
    [picker dismissViewControllerAnimated:YES completion:NULL];
}

@end

.imageOrientation如果您运行此代码,用手机在人像(是 3 = )中拍照UIImageOrientationRight并选择共享活动,如消息、保存图像等,图像方向是正确的。如果您选择复制活动,则粘贴到别处的图像会旋转(图像顶部在左侧)。

作为雷达 19456881提交。完整的项目可在此处获得。

4

2 回答 2

0

基于这篇文章,我决定创建自己的复制活动:

//
//  PhotoActivityCopy.m
//
//  Created by Jeff Vautin on 1/13/15.
//  Copyright (c) 2015 Jeff Vautin. All rights reserved.
//

#import "PhotoActivityCopy.h"
#import "ImageKit.h"
@import MobileCoreServices;

@interface PhotoActivityCopy ()

@property (strong,nonatomic) NSData *imageJPEGData;

@end

@implementation PhotoActivityCopy

NSString *PhotoActivityCopyActivityType = @"com.me.uiactivitytype.copy";
NSString *PhotoActivityCopyActivityTitle = @"Copy";

+ (UIActivityCategory)activityCategory
{
    return UIActivityCategoryAction;
}

- (NSString *)activityType
{
    return PhotoActivityCopyActivityType;
}

- (NSString *)activityTitle
{
    return PhotoActivityCopyActivityTitle;
}

- (UIImage *)activityImage
{
    CGSize imageSize;
    if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad) {
        imageSize = CGSizeMake(76.0, 76.0);
    } else {
        imageSize = CGSizeMake(60.0, 60.0);
    }

    return [ImageKit imageOfIconCopyWithExportSize:imageSize];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    BOOL canPerform = NO;

    if ([activityItems count] > 1) {
        canPerform = NO;
    } else if ([[activityItems firstObject] isKindOfClass:[UIImage class]]) {
        canPerform = YES;
    }

    return canPerform;
}

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    self.imageJPEGData = UIImageJPEGRepresentation(activityItems[0], 1.0);
}

- (void)performActivity
{
    [[UIPasteboard generalPasteboard] setData:self.imageJPEGData forPasteboardType:(id)kUTTypeJPEG];
    [self activityDidFinish:YES];
}

@end

然后我排除了本机复制活动:

PhotoActivityCopy *copyActivity = [[PhotoActivityCopy alloc] init];
UIActivityViewController *activityViewController = [[UIActivityViewController alloc] initWithActivityItems:@[chosenImage] applicationActivities:@[copyActivity]];
activityViewController.excludedActivityTypes = @[UIActivityTypeCopyToPasteboard];
于 2015-01-14T03:14:29.450 回答
0

默认情况下捕获图像方向行为是 UIImageOrientationRight,sp 你必须设置捕获图像方向是

[UIDevice currentDevice].orientation
于 2015-01-13T05:01:58.200 回答