无法调整图片大小以便以新尺寸保存

时间:2013-07-26 15:05:17

标签: objective-c image file resize save

我有以下代码

       if (! [url getResourceValue:&isDirectory forKey:NSURLIsDirectoryKey error:&error]) {
            // handle error
        }
        else if (! [isValidDirectory boolValue]) {
            // No error and it’s not a directory; do something with the file
            NSString* outP = [url absoluteString];
            NSString* extension = [outP substringFromIndex: outP.length-3];
            NSString* img = @"png";
            if([extension isEqualToString:img]){
                NSImage *original = [[NSImage alloc] initWithContentsOfURL: url];
                NSSize sizes = NSMakeSize(240, 240);
                NSImage *small =original;
                [small setSize:sizes];
                NSArray*  representations  = [small representations];
                NSInteger* total = [substrings count];
                NSData* bitmapData = [NSBitmapImageRep representationOfImageRepsInArray: representations usingType: NSPNGFileType properties:nil];
                [bitmapData writeToFile:@"/Users/testuser/downloads/test/test_tn.png" atomically:NO];
           }

当我运行它时,它没有调整图像的大小,它只是给我一个名为test_tn.png而不是较小图像的图像的直接副本。我不确定我做错了什么,这也是一个mac应用程序。那里有一些未使用的代码供以后使用。在我调整大小后,问题是我如何将NSImage传递给NSData?

编辑:确定所以我将NSData转换回NSImage,看起来NSData没有获得大小的数据而是原始数据。

编辑2:这样可行

好的,这样可行

            if([extension isEqualToString:img]){
                NSImage *image = [[NSImage alloc] initWithContentsOfURL: url];
                NSData *sourcedata =[image TIFFRepresentation];
                NSSize newSize;
                newSize.height = 160;
                newSize.width = 120;
                NSImage *sourceImage =[[NSImage alloc] initWithData: sourcedata];
                NSImage *resizedImage = [[NSImage alloc] initWithSize: NSMakeSize(240, 240)];

                NSSize originalSize = [sourceImage size];

                [resizedImage lockFocus];
                [sourceImage drawInRect: NSMakeRect(0, 0, 240, 240) fromRect: NSMakeRect(0, 0, originalSize.width, originalSize.height) operation: NSCompositeSourceOver fraction: 1.0];
                [resizedImage unlockFocus];

                NSData *resizedData = [resizedImage TIFFRepresentation];

                NSData *resizedPreviewData = [resizedImage TIFFRepresentation];
                NSBitmapImageRep *resizedCaptureImageBitmapRep = [[NSBitmapImageRep alloc] initWithData:resizedPreviewData];
                NSData* saveData = [resizedCaptureImageBitmapRep representationUsingType:NSPNGFileType properties:nil];
                [saveData writeToFile:@"/Users/testuser/downloads/test/test_tn.png" atomically:YES];
                count++;
            }

1 个答案:

答案 0 :(得分:0)

这对我有用。我使用此代码将图像叠加在一起,但它应该很容易适应您的目的:

CGSize destinationSize = smallSize;

UIGraphicsBeginImageContext(original.size);
[Original1 drawInRect:CGRectMake(0, 0, original.size.width, original.size.height)];
[Original2 drawInRect:CGRectMake(0, 0, original.size.width, original.size.height)];

UIImage *returnImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();

UIGraphicsBeginImageContext(destinationSize);
[returnImage drawInRect:CGRectMake(0,0,destinationSize.width,destinationSize.height)];
UIImage *smallImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();