UIActivityViewController复制忽略imageOrientation

时间:2015-01-13 04:56:03

标签: ios uiimage uiactivityviewcontroller uiimageorientation

当我在手机模式下使用手机拍照时,使用UIImagePickerController,返回的UIImage项目的图像顶部朝左,imageOrientation属性为UIImageOrientationRight , // 90 deg CCW,这与预期一样(I想)。

当我通过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)并选择共享活动,如消息,保存图像等,图像方向正确。如果选择复制活动,则会旋转粘贴到其他位置的图像(图像顶部位于左侧)。

提交为Radar 19456881。完整项目可用here

2 个答案:

答案 0 :(得分:0)

默认捕获图像方向行为是UIImageOrientation对,所以你必须设置捕获图像方向是

[UIDevice currentDevice].orientation

答案 1 :(得分:0)

基于this帖子,我决定创建自己的副本活动:

//
//  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];