代码之家  ›  专栏  ›  技术社区  ›  Rashad

使用PHImageManager调整图像大小以保持aspectfit并添加透明背景

  •  0
  • Rashad  · 技术社区  · 5 年前

    我想把图像的大小调整为正方形。如果原始图像不是正方形的,我想添加透明背景,使图像的纵横比保持不变。我的代码如下,我知道我现在没有做任何添加透明背景。有办法吗 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 回复  |  直到 5 年前
    推荐文章