在服务器上传图像后,设备上的图像大小不同

时间:2013-01-23 09:32:43

标签: iphone objective-c xcode uiimage alassetslibrary

我在上传图片之前正在进行以下操作。如果我在上传前检查图像大小,上传后它会加倍(即如果我上传了2 MB图像,我可以在服务器上看到4 MB图像)。

    NSTimeInterval timeInterval = [assetDate timeIntervalSince1970];
    ALAssetRepresentation *rep = [temp defaultRepresentation];
    CGImageRef iref = [rep fullResolutionImage];
    StrPath = [StrPath stringByAppendingFormat:@"%d.%@",(int)timeInterval,strImageType];

    UIImage *image =[UIImage imageWithCGImage:iref scale:[rep scale] orientation:(UIImageOrientation)[rep orientation]];
    NSData *dataObj = nil;
    dataObj = UIImageJPEGRepresentation(image, 1.0);
    NSString* StrFileData = [Base64 encode:dataObj];
    NSString* strFileHash = [dataObj md5Test];

2 个答案:

答案 0 :(得分:0)

使用以下方法将特定宽度图像

一起使用
+ (UIImage*)resizeImage:(UIImage*)image withWidth:(int)width withHeight:(int)height
{
    CGSize newSize = CGSizeMake(width, height);
    float widthRatio = newSize.width/image.size.width;
    float heightRatio = newSize.height/image.size.height;

    if(widthRatio > heightRatio)
    {
        newSize=CGSizeMake(image.size.width*heightRatio,image.size.height*heightRatio);
    }
    else
    {
        newSize=CGSizeMake(image.size.width*widthRatio,image.size.height*widthRatio);
    }


    UIGraphicsBeginImageContextWithOptions(newSize, NO, 0.0);
    [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    return newImage;
}

此方法返回 NewImage ,它不会是resiz:)

答案 1 :(得分:0)

我的建议很少。如果你注意到这些......它应该对你有帮助

  1. 您可以直接从ALAsset获取ALAsset图像NSData defaultRepresentation

    getBytes:fromOffset:length:error:方法。

  2. 使用ALAsset缩略图而不是检索全分辨率图像

    CGImageRef iref = [myasset aspectRatioThumbnail];

  3. ALAsset Libraay块将在单独的线程中执行。所以主线程中的服务器上传

     dispatch_async(dispatch_get_global_queue(0, 0), ^{
    
       // do the server upload
    
      });