使用PHImageManager调整图像大小并保持宽高比并添加透明背景

时间:2019-02-25 09:19:58

标签: ios objective-c phasset phimagemanager

我想将图像调整为正方形。如果原始图像不是正方形,我想为该图像的长宽比添加透明背景。我的代码如下,我知道我现在不做任何事情来添加透明背景。 requestImageForAsset是否可以通过默认方式执行类似的操作?

PHImageRequestOptions *requestOptions =  [[PHImageRequestOptions alloc] init];
            requestOptions.resizeMode   = PHImageRequestOptionsResizeModeExact;
            requestOptions.deliveryMode = PHImageRequestOptionsDeliveryModeHighQualityFormat;
            requestOptions.synchronous = true;
            NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
            NSString *documentsDirectory = [paths objectAtIndex:0];
            PHImageManager *manager = [PHImageManager defaultManager];

            [manager requestImageForAsset:_asset
                     targetSize:!_isLogo? LARGE_IMAGE_SIZE : COMPANY_LOGO_LARGE_SIZE
                     contentMode:PHImageContentModeAspectFit
                     options:requestOptions
                     resultHandler:^void(UIImage *image, NSDictionary *info) {

                         NSData *imageData = UIImagePNGRepresentation(image);
                         NSString *filename = [NSString stringWithFormat:@"/img_%f.png", [[Utils getCurrentDate] timeIntervalSince1970]];
                         NSString *tempPath = [documentsDirectory stringByAppendingString:filename];
                         [imageData writeToFile:tempPath atomically:YES];
                         largeImage = image;
            }];

0 个答案:

没有答案