Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c Can';无法将已调整大小的图像保存为新大小_Objective C_Image_File_Resize_Save - Fatal编程技术网

Objective c Can';无法将已调整大小的图像保存为新大小

Objective c Can';无法将已调整大小的图像保存为新大小,objective-c,image,file,resize,save,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 s

我有以下代码

       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++;
            }
好的,这样就行了

            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++;
            }

这对我有用。我正在使用此代码将图像分层,但它应该很容易适应您的目的:

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();

我不完全确定你的代码在做什么。它只是在调整大小吗?还有什么是原始1和2?以及如何在mac应用程序中导入UIImage