相关疑难解决方法(0)

如何将UIImage旋转90度?

我有一个UIImageUIImageOrientationUp(纵向),我想通过90度逆时针旋转(景观).我不想用CGAffineTransform.我想要UIImage实际转移位置的像素.我正在使用一段代码(如下所示),最初是为了调整大小UIImage来执行此操作.我将目标大小设置为当前大小UIImage但是我得到一个错误:

(错误):CGBitmapContextCreate:无效数据字节/行:对于8个整数位/组件,3个组件,kCGImageAlphaPremultipliedLast,应该至少为1708.

(每当我提供SMALLER尺寸作为目标尺寸BTW时,我都不会收到错误).如何UIImage在保留当前尺寸的同时仅使用核心图形功能旋转我的90度CCW?

-(UIImage*)reverseImageByScalingToSize:(CGSize)targetSize:(UIImage*)anImage
{
    UIImage* sourceImage = anImage; 
    CGFloat targetWidth = targetSize.height;
    CGFloat targetHeight = targetSize.width;

    CGImageRef imageRef = [sourceImage CGImage];
    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
    CGColorSpaceRef colorSpaceInfo = CGImageGetColorSpace(imageRef);

    if (bitmapInfo == kCGImageAlphaNone) {
        bitmapInfo = kCGImageAlphaNoneSkipLast;
    }

    CGContextRef bitmap;

    if (sourceImage.imageOrientation == UIImageOrientationUp || sourceImage.imageOrientation == UIImageOrientationDown) {
        bitmap = CGBitmapContextCreate(NULL, targetHeight, targetWidth, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpaceInfo, bitmapInfo);

    } else {


        bitmap = CGBitmapContextCreate(NULL, targetWidth, …
Run Code Online (Sandbox Code Playgroud)

core-graphics objective-c uikit uiimage ios

174
推荐指数
11
解决办法
17万
查看次数

iOS图像方向具有奇怪的行为

在过去的几周里,我一直在使用objective-c中的图像并注意到许多奇怪的行为.首先,像许多其他人一样,我一直遇到这个问题,用相机拍摄的图像(或用别人的相机和MMS给我拍摄)旋转90度.我不确定为什么这个世界会发生这种情况(因此我的问题),但我能够提出一个廉价的工作.

这次我的问题是为什么会发生这种情况?为什么Apple会旋转图像?当我用相机正面拍摄照片时,除非我执行上面提到的代码,否则当我保存照片时,它会被保存为旋转状态.现在,我的解决方法在几天前还可以.

我的应用程序修改的图像的各个像素,特别是PNG(所以任何JPEG转换被抛出窗外对我的情况)的alpha通道.几天前,我注意到即使图像在我的应用程序中正确显示,这要归功于我的解决方法代码,当我的算法修改图像的各个像素时,它认为图像是旋转的.因此,而不是在图像的顶部修改像素,它修改的图像侧的像素(因为它认为它应该被旋转)!我无法弄清楚如何在内存中旋转图像 - 理想情况下,我宁愿只是擦掉那个imageOrientation标志.

这是另一个令我困惑的东西......当我拍摄照片时,imageOrientation设置为3.我的解决方法代码非常智能,可以实现这一点并将其翻转,以便用户永远不会注意到.此外,我将图像保存到库中的代码实现了这一点,将其翻转,然后将其保存,使其正确显示在相机胶卷中.

该代码如下所示:

NSData* pngdata = UIImagePNGRepresentation (self.workingImage); //PNG wrap 
UIImage* img = [self rotateImageAppropriately:[UIImage imageWithData:pngdata]];   
UIImageWriteToSavedPhotosAlbum(img, nil, nil, nil);
Run Code Online (Sandbox Code Playgroud)

当我这个新保存的图像加载到我的应用程序,则imageOrientation是0 -正是我想看到的,而我的旋转解决办法甚至不需要运行(注:加载从互联网上的图像时,而不是用相机拍摄的图像,imageOrientation总是0,导致完美的行为).出于某种原因,我的保存代码似乎擦除了这个imageOrientation标志.我希望只是窃取该代码,并在用户拍照并将其添加到应用程序时使用它来擦除我的imageOrientation,但它似乎不起作用.有UIImageWriteToSavedPhotosAlbum什么特别的imageOrientation吗?

对于这个问题,最好的解决办法就是imageOrientation在用户完成拍摄图像后立即将其吹走.我认为Apple出于某种原因完成了旋转行为,对吧?一些人认为这是Apple的缺陷.

(...如果你还没有丢失......注2:当我拍摄水平照片时,一切似乎都很完美,就像从互联网上拍摄的照片一样)

编辑:

以下是一些图像和场景的实际情况.根据目前为止的评论,看起来这种奇怪的行为不仅仅是一种iPhone行为,我认为这种行为很好.

这是我带着我的手机(注意正确的方向)的照片的图片,它显示的样子,因为它没有我的电话时,我拍下照片:

在iPhone上拍摄的实际照片

以下是我通过电子邮件将图片发送给自己后的图片(看起来像Gmail正确处理):

照片显示在Gmail中

这是图像在Windows中作为缩略图的样子(看起来不像是正确处理):

Windows缩略图

以下是使用Windows Photo Viewer打开时的实际图像(仍未正确处理):

Windows照片查看器版本

在对这个问题的所有评论之后,这就是我在想的...... iPhone拍摄了一张图片,并说"要正确显示它,它需要旋转90度".此信息将在EXIF数据中.(为什么它需要旋转90度,而不是默认直线垂直,我不知道).从这里开始,Gmail非常智能,可以读取和分析EXIF数据,并正确显示它.但是,Windows不够智能,无法读取EXIF数据,因此显示图像不正确.我的假设是否正确?

iphone objective-c ios

88
推荐指数
7
解决办法
9万
查看次数

iOS - UIImageView - 如何处理UIImage图像方向

是否可以设置UIImageView来处理图像方向?当我将UIImageView设置为方向为RIGHT的图像(它是来自相机胶卷的照片)时,图像会向右旋转,但我想以正确的方向显示它.

我知道我可以旋转图像数据,但它可以做得更优雅吗?

谢谢

orientation uiimageview uiimage ios

74
推荐指数
9
解决办法
10万
查看次数

UIImagePNGRepresentation问题?/图像旋转90度

我想从UIImagePickerController加载图像,然后将选定的照片保存到我的应用程序的文档目录中.

UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
NSData *data1 = UIImagePNGRepresentation(image);

NSString *fileName = "1.png";
NSString *path = //get Document path, then add fileName
BOOL succ = [data1 writeToFile:path atomically:YES];
Run Code Online (Sandbox Code Playgroud)

但是在我将图像保存到我的文档后,我发现,图像旋转了90度,然后我将方法UIImagePNGRepresentation更改为UIImageJPEGRepresentation,这次没关系,有谁知道问题是什么?

iphone cocoa-touch uiimagepngrepresentation

48
推荐指数
4
解决办法
3万
查看次数

如何在Swift中旋转图像?

我无法将图像快速旋转90度.我写了下面的代码,但有一个错误,不编译

  func imageRotatedByDegrees(oldImage: UIImage, deg degrees: CGFloat) -> UIImage {

    //Calculate the size of the rotated view's containing box for our drawing space
    let rotatedViewBox: UIView = UIView(frame: CGRect(x: 0, y: 0, width: oldImage.size.width, height: oldImage.size.height))
    let t: CGAffineTransform = CGAffineTransform(rotationAngle: degrees * CGFloat(M_PI / 180))
    rotatedViewBox.transform = t
    let rotatedSize: CGSize = rotatedViewBox.frame.size

    //Create the bitmap context
    UIGraphicsBeginImageContext(rotatedSize)
    let bitmap: CGContext = UIGraphicsGetCurrentContext()!

    //Move the origin to the middle of the image so we will rotate and scale around …
Run Code Online (Sandbox Code Playgroud)

animation uiview uiviewanimation ios swift

45
推荐指数
7
解决办法
7万
查看次数

如何在iOS上将图像旋转90度?

我想要做的是从我的相机拍摄快照,将其发送到服务器,然后服务器将我的图像发送回viewController.如果图像处于纵向模式,则图像在屏幕上显示良好,但是如果图像是以横向模式拍摄的,则图像在屏幕上显示为拉伸(因为它试图以纵向模式显示!).我不知道如何解决这个问题,但我猜一个解决方案是首先检查图像是否处于纵向/横向模式,然后如果处于横向模式,则在将其显示在屏幕上之前将其旋转90度.那我该怎么办呢?

iphone objective-c rotation ios

43
推荐指数
6
解决办法
8万
查看次数

iOS:保存为PNG表示数据后,图像旋转90度

我已经研究了足够的工作,但无法修复它.只要我将图像存储为UIImage,从相机拍照后,它就可以了,但只要我将此图像存储为PNG表示,它就会旋转90度.

以下是我的代码和我尝试过的所有事情:

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info 
{
    NSString *mediaType = [info valueForKey:UIImagePickerControllerMediaType];

    if([mediaType isEqualToString:(NSString*)kUTTypeImage]) 
    {
        AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
        delegate.originalPhoto  = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        NSLog(@"Saving photo");
        [self saveImage];
        NSLog(@"Fixing orientation");
        delegate.fixOrientationPhoto  = [self fixOrientation:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];      
        NSLog(@"Scaling photo");
        delegate.scaledAndRotatedPhoto  =  [self scaleAndRotateImage:[UIImage imageWithContentsOfFile:[delegate filePath:imageName]]];
    }
    [picker dismissModalViewControllerAnimated:YES];
    [picker release];
}


- (void)saveImage
{
    AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
    NSData *imageData = UIImagePNGRepresentation(delegate.originalPhoto);
    [imageData writeToFile:[delegate filePath:imageName] atomically:YES];
}
Run Code Online (Sandbox Code Playgroud)

这里分别从这里这里取得fixOrientation和scaleAndRotateImage函数.当我在UIImage上应用它们时,它们工作正常并旋转图像,但如果我将图像保存为PNG表示并应用它们则不起作用.

请执行以上功能后请参考以下图片:

第一张照片是原始的,第二张是保存的,第三张和第四张是在保存的图像上应用fixorientation和scaleandrotate功能后

iphone image-rotation uiimage landscape-portrait ios5.1

38
推荐指数
4
解决办法
4万
查看次数

iPhone AVFoundation摄像头方向

我一直在试图让我的头发试图让AVFoundation相机以正确的方向(即设备方向)拍摄图片,但我无法让它工作.

我看过教程,我看过WWDC演示文稿,我已经下载了WWDC示例程序,但即便如此也没有.

我的应用程序的代码是......

AVCaptureConnection *videoConnection = [CameraVC connectionWithMediaType:AVMediaTypeVideo fromConnections:[imageCaptureOutput connections]];
if ([videoConnection isVideoOrientationSupported])
{
    [videoConnection setVideoOrientation:[UIApplication sharedApplication].statusBarOrientation];
}

[imageCaptureOutput captureStillImageAsynchronouslyFromConnection:videoConnection
                                                completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error)
{
    if (imageDataSampleBuffer != NULL)
    {
        //NSLog(@"%d", screenOrientation);

        //CMSetAttachment(imageDataSampleBuffer, kCGImagePropertyOrientation, [NSString stringWithFormat:@"%d", screenOrientation], 0);

        NSData *imageData = [AVCaptureStillImageOutput jpegStillImageNSDataRepresentation:imageDataSampleBuffer];
        UIImage *image = [[UIImage alloc] initWithData:imageData];

        [self processImage:image];
    }
}];
Run Code Online (Sandbox Code Playgroud)

(processImage使用与WWDC代码相同的writeImage ...方法)

并且WWDC应用程序的代码是......

AVCaptureConnection *videoConnection = [AVCamDemoCaptureManager connectionWithMediaType:AVMediaTypeVideo fromConnections:[[self stillImageOutput] connections]];
        if ([videoConnection isVideoOrientationSupported]) {
            [videoConnection setVideoOrientation:AVCaptureVideoOrientationPortrait];
        }

[[self stillImageOutput] captureStillImageAsynchronouslyFromConnection:videoConnection
                                                             completionHandler:^(CMSampleBufferRef imageDataSampleBuffer, NSError *error) {
                                                                 if …
Run Code Online (Sandbox Code Playgroud)

iphone camera avfoundation orientation

35
推荐指数
3
解决办法
3万
查看次数

将UIImage转换为cv :: Mat

我有一个UIImage,它是从iPhone相机拍摄的照片,现在我希望将UIImage转换为cv :: Mat(OpenCV).我使用以下代码行来完成此任务:

-(cv::Mat)CVMat
{

CGColorSpaceRef colorSpace = CGImageGetColorSpace(self.CGImage);
CGFloat cols = self.size.width;
CGFloat rows = self.size.height;

cv::Mat cvMat(rows, cols, CV_8UC4); // 8 bits per component, 4 channels

CGContextRef contextRef = CGBitmapContextCreate(cvMat.data,                 // Pointer to backing data
                                                cols,                      // Width of bitmap
                                                rows,                     // Height of bitmap
                                                8,                          // Bits per component
                                                cvMat.step[0],              // Bytes per row
                                                colorSpace,                 // Colorspace
                                                kCGImageAlphaNoneSkipLast |
                                                kCGBitmapByteOrderDefault); // Bitmap info flags

CGContextDrawImage(contextRef, CGRectMake(0, 0, cols, rows), self.CGImage);
CGContextRelease(contextRef);

return cvMat;
}
Run Code Online (Sandbox Code Playgroud)

此代码适用于横向模式下的UIImage,但当我使用与从纵向模式拍摄的图像相同的代码时,图像会向右旋转90度.

我是iOS和Objective C的新手,因此我无法弄清楚出了什么问题. …

opencv objective-c ios

31
推荐指数
2
解决办法
1万
查看次数

如何在objective-c中从cvMat转换为UIImage?

我正在使用OpenCV框架与XCode,并希望从cvMat或IplImage转换为UIImage,该怎么做?谢谢.

xcode opencv objective-c

28
推荐指数
4
解决办法
2万
查看次数