旋转存储在沙箱中的图像

时间:2011-08-10 09:22:26

标签: iphone objective-c cocoa-touch

我想旋转图像并保存以转到沙盒,但是图片方向的沙箱没有改变。为什么?请专家指导,谢谢!

以下是轮换代码:

-(void)leftRotate
{
        switch (i%4)
    {
        case 0:
            myImageView.image = [UIImage imageWithCGImage:myImageView.image.CGImage scale:1 orientation:UIImageOrientationUp];

            break;
        case 1:
            myImageView.image = [UIImage imageWithCGImage:myImageView.image.CGImage scale:1 orientation:UIImageOrientationLeft];

            break;
        case 2:
            myImageView.image = [UIImage imageWithCGImage:myImageView.image.CGImage scale:1 orientation:UIImageOrientationDown];

            break;
        case 3:
            myImageView.image = [UIImage imageWithCGImage:myImageView.image.CGImage scale:1 orientation:UIImageOrientationRight];

            break;

        default:
            break;
    }
    ++i;
}

以下是将图像保存到沙箱的代码:

-(void)save:(id)sender
{
    NSDate *date=[NSDate date];

    NSLog(@"%@",date);
    NSCalendar *calendar=[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
    NSInteger unitFlags=NSYearCalendarUnit|NSMonthCalendarUnit|NSDayCalendarUnit|NSHourCalendarUnit|NSMinuteCalendarUnit|NSSecondCalendarUnit;
    NSDateComponents *component=[calendar components:unitFlags fromDate:date];
    int year=[component year];
    int month=[component month];
    int day=[component day];
    int h=[component hour];
    int m=[component minute];
    int s=[component second];
    fileName=[NSString stringWithFormat:@"%d-%d-%d_%d:%d:%d.png",year,month,day,h,m,s];
    NSLog(@"%@",fileName);
    NSArray *paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path=[paths objectAtIndex:0];
    NSLog(@"path:%@",path);
    filePath=[path stringByAppendingPathComponent:fileName];
    NSLog(@"filePath:%@",filePath); 
    NSData *data=[NSData dataWithData:UIImagePNGRepresentation(myImageView.image)];
    [data writeToFile:filePath atomically:YES];       
}

1 个答案:

答案 0 :(得分:1)

PNG文件不支持旋转标志(仅保存在文件的元数据中,像素不会更改)。您需要将图像保存为JPEG以使其以这种方式旋转,或者旋转实际的像素数据。